code / nomsu

Lines6.6K Lua5.1K PEG1.3K make117
2 others 83
Markdown60 Bourne Again Shell23
(183 lines)
1 -- Nomsu version 1
2 file (FileChunks) <-
3 {:curr_indent: ' '* :}
4 ("#!" (!"nomsu" [^%nl])* "nomsu" ws+ "-V" ws* {:version: [0-9.]+ :} [^%nl]*)?
5 comment? blank_lines?
6 (chunk (nl_nodent chunk_delimeter nl_nodent chunk)*)?
7 blank_lines?
8 {:curr_indent: %nil :}
9 !.
11 nodent <- (unexpected_indent [^%nl]* / =curr_indent)
12 indent <- =curr_indent " "
13 blank_lines <- %nl ((nodent comment / ws*) %nl)*
14 eol <- ws* eol_comment? (!. / &%nl)
16 nl_nodent <- blank_lines nodent
17 nl_indent <- blank_lines {:curr_indent: indent :} (comment nl_nodent)*
19 comment (Comment) <-
20 "#" {~ [^%nl]* (%nl+ (indent -> '') [^%nl]*)* ~}
21 eol_comment (Comment) <-
22 "#" {[^%nl]*}
24 unexpected_code <- ws* _unexpected_code
25 _unexpected_code (Error) <-
26 {:error: {~ [^%nl]+ -> "Couldn't parse this code" ~} :}
27 unexpected_indent (Error) <-
28 {:error: {~ (=curr_indent ws+) -> "Messed up indentation" ~} :}
29 {:hint: {~ '' -> 'Either make sure this line is aligned with the one above it, or make sure the previous line ends with something that uses indentation, like ":" or "(..)"' ~} :}
30 missing_paren_err (Error) <-
31 {:error: {~ eol -> 'Line ended without finding a closing )-parenthesis' ~} :}
32 {:hint: {~ '' -> 'Put a ")" here' ~} :}
33 missing_quote_err (Error) <-
34 {:error: {~ eol -> 'Line ended before finding a closing double quotation mark' ~} :}
35 {:hint: {~ "" -> "Put a quotation mark here" ~} :}
36 missing_bracket_error (Error) <-
37 {:error: {~ eol -> "Line ended before finding a closing ]-bracket" ~} :}
38 {:hint: {~ '' -> 'Put a "]" here' ~} :}
39 missing_brace_error (Error) <-
40 {:error: {~ eol -> "Line ended before finding a closing }-brace" ~} :}
41 {:hint: {~ '' -> 'Put a "}" here' ~} :}
42 missing_block_expr_error (Error) <-
43 {:error: '' -> "Missing expression after the ':'" :}
45 chunk <- block / action / expression
46 chunk_delimeter <- ("~")^+3 eol
48 inline_block (Block) <-
49 inline_statement (ws* ";" ws* inline_statement)+
50 block (Block) <-
51 statement (nl_nodent statement)+
52 (%nl (ws* %nl)* nodent (comment / eol / unexpected_code))*
53 {:curr_indent: %nil :}
55 statement <- (action / expression) (eol / unexpected_code)
56 inline_statement <- (inline_action / inline_expression)
58 noindex_inline_expression <-
59 number / variable / inline_text / inline_list / inline_dict / inline_nomsu
60 / ( "("
61 ws* (inline_block / inline_action / inline_expression) ws*
62 (ws* ',' ws* (inline_block / inline_action / inline_expression) ws*)*
63 (")" / missing_paren_err / unexpected_code)
65 inline_expression <- index_chain / noindex_inline_expression
66 indented_expression <-
67 indented_text / indented_nomsu / indented_list / indented_dict / ({|
68 ("(..)")? nl_indent
69 (block / action / expression) (eol / unexpected_code)
70 (%nl (ws* %nl)* nodent (comment / eol / unexpected_code))*
71 {:curr_indent: %nil :}
72 |} -> unpack)
73 expression <-
74 inline_expression
75 / (":" ws*
76 (inline_block / inline_action / inline_expression / missing_block_expr_error))
77 / indented_expression
79 inline_nomsu (EscapedNomsu) <- "\" inline_expression
80 indented_nomsu (EscapedNomsu) <-
81 "\" (
82 noindex_inline_expression
83 / (":" ws*
84 (inline_block / inline_action / inline_expression / missing_block_expr_error))
85 / indented_expression)
87 index_chain (IndexChain) <-
88 noindex_inline_expression ("." (text_word / noindex_inline_expression))+
90 -- Actions need either at least 1 word, or at least 2 tokens
91 inline_action (Action) <-
92 !chunk_delimeter
93 ( (inline_expression (ws* (inline_expression / word))+)
94 / (word (ws* (inline_expression / word))*))
95 (ws* ":" ws* (inline_block / inline_action / inline_expression
96 / missing_block_expr_error))?
97 action (Action) <-
98 !chunk_delimeter
99 ( (expression ((nl_nodent "..")? ws* (expression / word))+)
100 / (word ((nl_nodent "..")? ws* (expression / word))*))
102 word <- !number { operator_char+ / ident_char+ }
104 text_word (Text) <- word
106 inline_text (Text) <-
107 !(indented_text)
108 '"'
109 ({~ (('\"' -> '"') / ('\\' -> '\') / escaped_char / [^%nl\"])+ ~}
110 / inline_text_interpolation)*
111 ('"' / missing_quote_err / unexpected_code)
112 inline_text_interpolation <-
113 "\" (
114 variable / inline_list / inline_dict / inline_text
115 / ("("
116 ws* (inline_block / inline_action / inline_expression) ws*
117 (ws* ',' ws* (inline_block / inline_action / inline_expression) ws*)*
118 (")" / missing_paren_err / unexpected_code))
121 indented_text (Text) <-
122 '".."' eol %nl {%nl+}? {:curr_indent: indent :}
123 (indented_plain_text / text_interpolation / {~ %nl+ (=curr_indent -> "") ~})*
124 unexpected_code?
125 {:curr_indent: %nil :}
126 indented_plain_text (Text) <-
127 {~ (("\\" -> "\") / (("\" blank_lines =curr_indent "..") -> "") / (!text_interpolation "\") / [^%nl\]+)+
128 (%nl+ (=curr_indent -> ""))* ~}
129 text_interpolation <-
130 inline_text_interpolation / ("\" indented_expression blank_lines =curr_indent "..")
132 number (Number) <- (("-"? (([0-9]+ "." [0-9]+) / ("." [0-9]+) / ([0-9]+)))-> tonumber)
134 -- Variables can be nameless (i.e. just %) and can't contain operators like apostrophe
135 -- which is a hack to allow %'s to parse as "%" and "' s" separately
136 variable (Var) <- "%" {(ident_char+ ((!"'" operator_char+) / ident_char+)*)?}
138 inline_list (List) <-
139 !('[..]')
140 "[" ws*
141 (inline_list_item (ws* ',' ws* inline_list_item)* (ws* ',')?)? ws*
142 ("]" / (","? (missing_bracket_error / unexpected_code)))
143 indented_list (List) <-
144 "[..]" eol nl_indent
145 list_line (nl_nodent list_line)*
146 (%nl (ws* %nl)* nodent (comment / eol / unexpected_code))*
147 (","? unexpected_code)?
148 list_line <-
149 (inline_list_item ws* "," ws*)+ eol
150 / (inline_list_item ws* "," ws*)* (action / expression) eol
151 inline_list_item <- inline_block / inline_action / inline_expression
153 inline_dict (Dict) <-
154 !('{..}')
155 "{" ws*
156 (inline_dict_entry (ws* ',' ws* inline_dict_entry)*)? ws*
157 ("}" / (","? (missing_brace_error / unexpected_code)))
158 indented_dict (Dict) <-
159 "{..}" eol nl_indent
160 dict_line (nl_nodent dict_line)*
161 (%nl (ws* %nl)* nodent (comment / eol / unexpected_code))*
162 (","? unexpected_code)?
163 dict_line <-
164 (inline_dict_entry ws* "," ws*)+ eol
165 / (inline_dict_entry ws* "," ws*)* dict_entry eol
166 dict_entry(DictEntry) <-
167 dict_key (ws* ":" ws* (action / expression))?
168 inline_dict_entry(DictEntry) <-
169 dict_key (ws* ":" ws* (inline_block / inline_action / inline_expression)?)?
170 dict_key <-
171 text_word / inline_expression
173 operator_char <- ['`~!@$^&*+=|<>?/-]
174 ident_char <- [a-zA-Z0-9_] / %utf8_char
175 ws <- [ %tab]
177 escaped_char <-
178 ("\"->'') (
179 (([xX]->'') ((({[0-9a-fA-F]^2} %number_16) -> tonumber) -> tochar))
180 / ((([0-9] [0-9]^-2) -> tonumber) -> tochar)
181 / ("a"->ascii_7) / ("b"->ascii_8) / ("t"->ascii_9) / ("n"->ascii_10)
182 / ("v"->ascii_11) / ("f"->ascii_12) / ("r"->ascii_13)