123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 | -- Nomsu version 8
file <-
{:curr_indent: ' '* :}
(((comment / methodchain / action / expression / inline_block) eol !.) / whole_file)
{:curr_indent: %nil :}
!.
shebang <- "#!" [^%nl]*
whole_file (File) <-
{:shebang: shebang :}?
(file_statements / blank_lines? comment? blank_lines?)
unexpected_indent? unexpected_chunk?
file_statements <-
((blank_lines nodent) / (comment nl_nodent))? statement (nl_nodent statement)* blank_lines?
-- Nomsu 7 used lines of "~~~" to split files into sections that ran separately
removed_section_division (Error) <-
{:error: {~ (("~")^+3 ws*) -> "Section dividers (~~~) aren't used in Nomsu 8." ~} :}
{:hint: {~ '' -> "Remove this line. Compile rules can now be used anywhere after they're defined." ~} :}
eof <- !.
eol <- ws* (&%nl / !.)
nodent <- (unexpected_indent [^%nl]* / =curr_indent)
indent <- { =curr_indent " " }
blank_lines <- %nl ((tab_error / nodent comment / ws*) %nl)*
nl_nodent <- blank_lines nodent
nl_indent <- blank_lines {:curr_indent: indent :} (comment nl_nodent)*
comment (Comment) <-
"###" {~ [^%nl]* (%nl+ (indent -> '') [^%nl]*)* (%nl &%nl)* ~}
inline_block (Block) <-
":" ws* inline_statement?
(&eol !nl_indent / &(ws* [)},]))
indented_block (Block) <-
":" eol nl_indent statement (nl_nodent statement)*
(%nl (ws* %nl)* nodent (comment / eol / unexpected_code))*
{:curr_indent: %nil :}
statement <-
(removed_section_division / methodchain / action / expression)
(eol / unexpected_code)
inline_statement <-
inline_methodchain / inline_action / inline_expression
noindex_inline_expression <-
number / variable / inline_text / inline_table / inline_unary_action /
"("
ws* (inline_block / inline_methodchain / inline_action / inline_expression) ws*
(")" / eof / missing_paren_err / unexpected_code)
inline_expression <- inline_index_chain / noindex_inline_expression / inline_block
indented_expression <-
indented_text / indented_table / indented_block /
indented_parens / unary_action
indented_parens <-
"(" indented_naked_expression (nl_nodent ")" / missing_paren_err / unexpected_code)
indented_naked_expression <-
({| nl_indent
(methodchain / action / expression) (eol / unexpected_code)
(%nl (ws* %nl)* nodent (comment / eol / unexpected_code))*
{:curr_indent: %nil :}
|} -> unpack)
expression <-
inline_expression / indented_expression
inline_index (Index) <-
"." (hex_integer / integer / text_word / noindex_inline_expression)
_inline_index (IndexChain) <- inline_index
inline_index_chain <-
(noindex_inline_expression _inline_index+) -> foldr
-- Actions need 1 argument and either another argument or a word.
inline_action (Action) <-
( inline_expression (ws* inline_arg)+
/ word (ws* inline_arg)*)
inline_arg <- inline_expression / word
inline_unary_action (Action) <-
%at_break operator !":" inline_expression
action (Action) <-
( (expression / indented_naked_expression) ((linesplit / ws*) arg)+
/ word ((linesplit / ws*) arg)*)
arg <- expression / indented_naked_expression / word
linesplit <- eol nl_nodent ".." ws*
unary_action (Action) <-
%at_break operator !":" !eol (expression / indented_naked_expression)
inline_methodsuffix (MethodCall) <- inline_action
inline_methodchain <-
((inline_action / inline_expression) (ws* "," ws* inline_methodsuffix)+) -> foldr
methodsuffix (MethodCall) <- action
methodchain <-
((unary_action / inline_action / expression) ((linesplit / ws*) "," ws* methodsuffix)+) -> foldr
word <- !number { %operator_char+ / ident_char+ }
operator <- !"###" {%operator_char+}
text_word (Text) <- word
inline_text (Text) <-
'"' !eol _inline_text* ('"' / eof / missing_quote_err / unexpected_code)
_inline_text <-
{~ (('\"' -> '"') / ('\\' -> '\') / escaped_char / text_char+)+ ~}
/ inline_text_interpolation / illegal_char
inline_text_interpolation <-
"\" (
variable / inline_table
/ ("("
ws* ((inline_methodchain / inline_action / inline_expression) ws*)?
(")" / eof / missing_paren_err / unexpected_code))
)
text_char <- %utf8_char / !["\] %print / %tab
indented_text (Text) <-
'("' %nl {%nl*} ({|
{:curr_indent: indent :}
(removed_text_continuation / indented_plain_text / text_interpolation / illegal_char
/ blank_text_lines)*
{:curr_indent: %nil :}
|} -> unpack)
(nl_nodent '")' / eof / missing_indented_quote_err)
-- Tracking text-lines-within-indented-text as separate objects allows for better debugging line info
-- The only escapes in multi-line text are "\\" for a backslash and "\" followed by a value.
-- A line that starts with "...." where the text's indentation would be continues the line above.
indented_plain_text (Text) <-
{~
(((%nl %text_continuation) -> "") / ('\\' -> '\')
/ (!text_interpolation !old_text_continuation '\')
/ ('"' / text_char)+)+
blank_text_lines?
~}
-- Before Nomsu 8, a line of text ending in "\" was continued by a line starting with ".."
old_text_continuation <- "\" blank_lines =curr_indent ".."
removed_text_continuation (Error) <-
&old_text_continuation
{:error: {~ "\" -> 'Lines of text are continued with "...." now.' ~} :}
{:hint: {~ '' -> 'Remove the "\" here, and start the next line with "...." in place of the last 4 spaces of its indentation (with "\\" for a backslash at the end of a line).' ~} :}
blank_text_lines <-
{~ (%nl ((ws* -> '') (&%nl / !.) / (=curr_indent -> '') &[^%nl]))+ ~}
text_interpolation <-
({|
-- %indentation will backtrack and match the actual indentation of the current line
"\" {:curr_indent: %indentation :} !":" indented_expression
|} -> unpack)
/ inline_text_interpolation
number <-
hex_integer / real_number / integer
integer (Number) <-
{ (%at_break "-")? [0-9]+ (("_"+->"") [0-9]+)* }
hex_integer (Number) <-
{ (%at_break "-")? "0x" [0-9a-fA-F]+ (("_"+->"") [0-9a-fA-F]+)* }
real_number (Number) <-
{ (%at_break "-")? [0-9]+ (("_"+->"") [0-9]+)* "." [0-9]+ (("_"+->"") [0-9]+)* }
variable (Var) <- "$" ({ident_char+} / "(" ws* (inline_methodchain / inline_action / variable) ws* ")" / {''})
inline_table (Table) <-
!indented_table
"{" ws* !eol
(inline_table_item (ws* ',' ws* inline_table_item)* (ws* ',')?)? ws*
("}" / eof / (","? (missing_brace_error / unexpected_code)))
indented_table (Table) <-
({|
"{" eol nl_indent table_line (nl_nodent table_line)*
(%nl (ws* %nl)* nodent (comment / eol / unexpected_code))*
{:curr_indent: %nil :}
|} -> unpack)
(nl_nodent "}" / eof / missing_brace_error / unexpected_code)
table_line <-
(inline_table_item ws* "," ws*)+ eol
/ (inline_table_item ws* "," ws*)* (table_entry / inline_index / action / expression) eol
-- A key on its own, like {.x}, is short for {.x = (yes)}, and is only allowed in a table
inline_table_item <- inline_table_entry / inline_index / inline_action / inline_expression
inline_table_entry (TableEntry) <-
inline_index ws* "=" !%operator_char ws* (inline_action / inline_expression)
table_entry (TableEntry) <-
inline_index ws* "=" !%operator_char ws* (action / expression / indented_naked_expression)
ident_char <- [a-zA-Z0-9_] / (!%operator_char %utf8_char)
ws <- " "
escaped_char <-
("\"->'') (
(([xX]->'') ((({[0-9a-fA-F]^2} %number_16) -> tonumber) -> tochar))
/ ((([0-9] [0-9]^-2) -> tonumber) -> tochar)
/ ("a"->ascii_7) / ("b"->ascii_8) / ("t"->ascii_9) / ("n"->ascii_10)
/ ("v"->ascii_11) / ("f"->ascii_12) / ("r"->ascii_13)
)
-- Errors
unexpected_code <- ws* _unexpected_code
_unexpected_code (Error) <-
{:error: {~ [^%nl]+ -> "Couldn't parse this code." ~} :}
unexpected_chunk (Error) <-
{:error: {~ .+ -> "Couldn't parse this chunk of code." ~} :}
unexpected_indent (Error) <-
{:error: {~ (=curr_indent ws+) -> "This indentation is messed up." ~} :}
{:hint: {~ '' -> 'This line should either have the same indentation as the line above it, or exactly 4 spaces more.' ~} :}
missing_paren_err (Error) <-
{:error: {~ eol -> 'This expression is missing a closing )-parenthesis.' ~} :}
{:hint: {~ '' -> 'Put a ")" here' ~} :}
missing_quote_err (Error) <-
{:error: {~ eol -> "This text is missing a closing quotation mark." ~} :}
{:hint: {~ "" -> "Put a quotation mark here." ~} :}
missing_indented_quote_err (Error) <-
{:error: {~ '' -> 'This text is missing a closing ")-quotation mark.' ~} :}
{:hint: {~ "" -> 'Put a ") after this line, at the same level of indentation as the opening (".' ~} :}
missing_brace_error (Error) <-
{:error: {~ eol -> "This table is missing a closing }-brace" ~} :}
{:hint: {~ '' -> 'Put a "}" here' ~} :}
tab_error <- ws* _tab_error [^%nl]*
_tab_error (Error) <-
{:error: {~ %tab+ -> 'Tabs are not allowed for indentation.' ~} :}
{:hint: {~ '' -> 'Use 4-space indentation instead of tabs.' ~} :}
illegal_char (Error) <-
{:error: {~ (!(%nl / %tab / %print) .) -> "Illegal unprintable character here (it may not be visible, but it's there)" ~} :}
{:hint: {~ '' -> "This sort of thing can happen when copying and pasting code. Try deleting and retyping the code." ~} :}
|