diff options
Diffstat (limited to 'nomsu.peg')
| -rw-r--r-- | nomsu.peg | 116 |
1 files changed, 60 insertions, 56 deletions
@@ -1,38 +1,37 @@ -- Nomsu version 2 file (File): - {:curr_indent: '' :} - blank_line* + %nl* + {:curr_indent: ' '* :} (chunk (nl_nodent chunk_delimeter nl_nodent chunk)*)? - blank_line* - (!. / (!! .* -> "Parse error" !!)) + %nl* + (!! .+ -> "Parse error" !!)? nodent: =curr_indent !(" ") -indent: =curr_indent " " !(" ") -dedent: !(=curr_indent) (" ")* -eol: %ws* (!. / &%nl) +indent: =curr_indent " " +blank_lines: %nl (%ws* %nl)* +eol: (!. / &%nl) comment (Comment): - "#" {~ [^%nl]* (%nl+ ({:curr_indent: indent :} -> '') [^%nl]* (%nl+ (=curr_indent -> '') [^%nl]*)* (!. / nl_dedent))? ~} + !inline_comment + "#" {~ [^%nl]* (blank_lines (indent -> '') [^%nl]*)* ~} inline_comment (Comment): - "(#" {~ (inline_comment / [^%nl])* ~} "#)" + "#(" {~ (inline_comment / [^%nl])* ~} ")#" -blank_line: %nl eol -nl_nodent: blank_line* %nl nodent -nl_indent: blank_line* %nl {:curr_indent: indent :} -nl_dedent: blank_line* %nl &dedent +nl_nodent: blank_lines nodent +nl_indent: blank_lines {:curr_indent: indent :} -chunk: !chunk_delimeter (block/action/expression) +chunk: block / ((comment nl_nodent)* (action / expression)? (nl_nodent comment)*) chunk_delimeter: ("~")^+3 inline_block (Block): - (inline_comment / inline_statement) (%ws* ";" %ws* (inline_comment / inline_statement))+ + inline_statement (%ws* ";" %ws* inline_statement)+ (%ws* inline_comment)* block (Block): - block_line (nl_nodent block_line)+ + block_line (nl_nodent block_line)+ (nl_nodent comment)* block_line: - comment / inline_comment / statement / (!! [^%nl]* -> "Unexpected character while parsing block line" !!) + statement %ws* (!! [^%nl]+ -> "Unexpected character while parsing block line" !!)? -statement: (action / expression) (eol / (!! [^%nl]* -> "Unexpected character while parsing line" !!)) -inline_statement: inline_action / inline_expression +statement: (comment nl_nodent)* (action / expression) %ws* (!! [^%nl]+ -> "Unexpected character while parsing line" !!)? +inline_statement: (inline_comment %ws*)* (inline_action / inline_expression) noindex_inline_expression: number / variable / inline_text / inline_list / inline_dict / inline_nomsu @@ -40,29 +39,32 @@ noindex_inline_expression: %ws* (inline_block / inline_action / inline_expression) %ws* (%ws* ',' %ws* (inline_block / inline_action / inline_expression) %ws*)* (")" - / (!! eol -> 'Line ended without finding a closing )-parenthesis' !!) + / (!! %ws* eol -> 'Line ended without finding a closing )-parenthesis' !!) / (!! [^%nl]+ -> 'Unexpected character while parsing subexpression' !!) ) ) inline_expression: index_chain / noindex_inline_expression indented_expression: - indented_text / indented_nomsu / indented_list / indented_dict - / (("(..)" / ":")? nl_indent - (block / action / expression) - (!. / &nl_dedent / (!! (!nl_dedent .)* -> "Unexpected character while parsing indented expression" !!)) - ) + indented_text / indented_nomsu / indented_list / indented_dict / {| + ("(..)" / ":")? nl_indent (comment nl_nodent)* + (block / action / expression) + (!! [^%nl]+ -> "Unexpected character while parsing indented expression" !!)? + {:is_halfblock: ''->'yep' :} + |} expression: inline_expression - / (":" %ws* ((inline_block / inline_action / inline_expression) eol - / (!! eol -> "Missing expression after the ':'" !!))) + / (":" %ws* + (!! eol -> "Missing expression after the ':'" !!)? + (inline_block / inline_action / inline_expression) %ws* eol) / indented_expression inline_nomsu (EscapedNomsu): "\" inline_expression indented_nomsu (EscapedNomsu): "\" ( noindex_inline_expression - / (":" %ws* ((inline_block / inline_action / inline_expression) eol - / (!! eol -> "Missing expression after the ':'" !!))) + / (":" %ws* + (!! eol -> "Missing expression after the '\:'" !!)? + (inline_block / inline_action / inline_expression) %ws* eol) / indented_expression) index_chain (IndexChain): @@ -70,26 +72,30 @@ index_chain (IndexChain): -- Actions need either at least 1 word, or at least 2 tokens inline_action (Action): - ( (inline_expression (%ws* (inline_expression / word))+) - / (word (%ws* (inline_expression / word))*)) - (%ws* ":" %ws* (inline_block / inline_action / inline_expression + !chunk_delimeter + (inline_comment %ws*)* + ( (inline_expression ((%ws* inline_comment)* %ws* (inline_expression / word))+) + / (word ((%ws* inline_comment)* %ws* (inline_expression / word))*)) + ((%ws* inline_comment)* %ws* ":" %ws* (inline_block / inline_action / inline_expression / (!! '' -> "Missing expression after the ':'" !!)))? action (Action): - (expression ((nl_nodent "..")? %ws* (expression / word))+) - / (word ((nl_nodent "..")? %ws* (expression / word))*) + !chunk_delimeter + (inline_comment %ws*)* + ( (expression (((nl_nodent comment)* nl_nodent "..")? %ws* (inline_comment %ws*)* (expression / word))+) + / (word (((nl_nodent comment)* nl_nodent "..")? %ws* (inline_comment %ws*)* (expression / word))*)) -word: !number ( %operator_char+ / %ident_char+ ) +word: !number { %operator_char+ / %ident_char+ } text_word (Text): word inline_text (Text): - !('".."' eol) + !('".."' %ws* eol) '"' ({~ (('\"' -> '"') / ('\\' -> '\') / %escaped_char / [^%nl\"])+ ~} / inline_text_interpolation)* ('"' / (!! eol -> 'Line ended before finding a closing double quotation mark' !!) - / (!! [^%nl]* -> 'Unexpected character while parsing Text' !!)) + / (!! [^%nl]+ -> 'Unexpected character while parsing Text' !!)) inline_text_interpolation: "\" ( variable / inline_list / inline_dict / inline_text @@ -97,21 +103,19 @@ inline_text_interpolation: %ws* (inline_block / inline_action / inline_expression) %ws* (%ws* ',' %ws* (inline_block / inline_action / inline_expression) %ws*)* (")" - / (!! &%nl -> 'Line ended without finding a closing )-parenthesis' !!) - / (!! [^%nl]* -> 'Unexpected character while parsing Text interpolation' !!))) + / (!! %ws* eol -> 'Line ended without finding a closing )-parenthesis' !!) + / (!! [^%nl]+ -> 'Unexpected character while parsing Text interpolation' !!))) ) --- Have to use "%indent" instead of "indent" etc. to avoid messing up text lines that start with "#" indented_text (Text): - '".."' eol %nl - {~ (%nl*) ({:curr_indent: indent :} -> "") ~} - (indented_plain_text / text_interpolation / {~ %nl+ (nodent -> "") ~})* - (!. / &nl_dedent / (!! (!nl_dedent .)* -> "Unexpected character while parsing Text" !!)) + '".."' %ws* %nl {:curr_indent: indent :} + (indented_plain_text / text_interpolation / {~ blank_lines (=curr_indent -> "") ~})* + (!! [^%nl]+ -> "Unexpected character while parsing Text" !!)? indented_plain_text (Text): - {~ (("\\" -> "\") / (("\" nl_nodent "..") -> "") / (!text_interpolation "\") / [^%nl\]+)+ - (%nl+ (nodent -> ""))* ~} + {~ (("\\" -> "\") / (("\" (blank_lines =curr_indent comment)* blank_lines =curr_indent "..") -> "") / (!text_interpolation "\") / [^%nl\]+)+ + (blank_lines (=curr_indent -> ""))* ~} text_interpolation: - inline_text_interpolation / ("\" indented_expression nl_nodent "..") + inline_text_interpolation / ("\" indented_expression blank_lines =curr_indent "..") number (Number): (("-"? (([0-9]+ "." [0-9]+) / ("." [0-9]+) / ([0-9]+)))-> tonumber) @@ -124,15 +128,15 @@ inline_list (List): "[" %ws* (inline_list_item (%ws* ',' %ws* inline_list_item)* (%ws* ',')?)? %ws* ("]" / (","? ( - (!! eol -> "Line ended before finding a closing ]-bracket" !!) - /(!! [^%nl]* -> "Unexpected character while parsing List" !!) + (!! %ws* eol -> "Line ended before finding a closing ]-bracket" !!) + /(!! [^%nl]+ -> "Unexpected character while parsing List" !!)? ))) indented_list (List): - "[..]" nl_indent + "[..]" nl_indent (comment nl_nodent)* list_line (nl_nodent list_line)* - (&nl_dedent / (","? (!! (!nl_dedent .)* -> "Unexpected character while parsing List" !!))) + (","? (!! [^%nl]+ -> "Unexpected character while parsing List" !!))? list_line: - (inline_list_item %ws* "," %ws*)+ eol + (inline_list_item %ws* "," %ws*)+ %ws* eol / (inline_list_item %ws* "," %ws*)* (action / expression) inline_list_item: inline_block / inline_action / inline_expression @@ -145,11 +149,11 @@ inline_dict (Dict): / (!! [^%nl]* -> "Unexpected character while parsing Dictionary" !!) ))) indented_dict (Dict): - "{..}" nl_indent - dict_line (nl_nodent dict_line)* - (&nl_dedent / (","? (!! (!nl_dedent .)* -> "Unexpected character while parsing Dictionary" !!))) + "{..}" nl_indent (comment nl_nodent)* + dict_line ((nl_nodent comment)* nl_nodent dict_line)* (nl_nodent comment)* + (","? (!! [^%nl]+ -> "Unexpected character while parsing Dictionary" !!))? dict_line: - (inline_dict_entry %ws* "," %ws*)+ eol + (inline_dict_entry %ws* "," %ws*)+ %ws* eol / (inline_dict_entry %ws* "," %ws*)* dict_entry dict_entry(DictEntry): dict_key (%ws* ":" %ws* (action / expression))? |
