-s = !ws (grammar::Grammar) !ws
+//funkanomitron
+s = ws Grammar ws
-Grammar = NonTerminal +/ ws
+Grammar:: = Declaration +/ ws
-NonTerminal = Word ^"=" RHS /ws
+Declaration = NonTerminal
+ | ^"#import" FileName /ws
+ | ^"#import" FileName "as" Word /ws
-// use 'a'-'z' or 'a-z' instead of [a-z]?
-// EOF token?
-// #include (with renaming?)
-// ANTLR uses ! and ^ suffixes
-
-RHS = (Sequence +/ (!ws "|" !ws)) +/ (!ws ">" !ws)
+FileName:: = (~[\r\n ] | escaped)+ -> [\r\n ]
-Elements = e*/ws
+NonTerminal = NonTerminal:: Word ws "=" ws RHS
+ | DropNT:: Word "!" ws "=" ws RHS
+ | Colons:: Word "::" ws "=" ws RHS
+ | Word "*" ws ^"=" ws RHS
+ | Word "*/" Word ws ^"=" ws RHS
+
+RHS:: = ("|":: Sequence +/ (ws "|" ws)) +/ (ws (">" -> ~">") ws)
-PreSequence = ps:: Elements
- | (Quoted|Word) ^"::" PreSequence /ws
- > PreSequence ^"/" e /ws
- | PreSequence ^"->" e /ws
+Elements:: = e*/ws
-Sequence = psx:: PreSequence
- | Sequence ^"&" Elements /ws
- | Sequence ^"&~" Elements /ws
+PreSequence = Elements
+ | (Quoted|Word) ^"::" PreSequence /ws
+ > PreSequence ^"/" e /ws
+ | PreSequence ^"->" e /ws
-ec = ~[\-\]\\]
- | escaped
-
-Range = range:: ec
- | ec ^"-" ec
+Sequence = PreSequence
+ | Sequence ^"&" Elements /ws
+ | ^"~~" Elements /ws
+ | Sequence ^"&~" Elements /ws
-e = (Quoted|Word) ^":" e
- > nonTerminal:: Word
- | literal:: Quoted
+e = e ^"!"
+ > (Quoted|Word) ^":" e
+ > NonTerminalReference:: Word
+ | Literal:: Quoted
| ^"()"
| ^"{" PreSequence "}" /ws
| ^"[" Range* "]"
| e ^"*" /ws -> ~[*]
| e ^"**/" e /ws
| e ^"*/" e /ws
- | ^"!" e /ws
| e ^"?" /ws
| ^"^" Quoted
- | "(" Word ^")" /ws
+ | ^"`" e
+ | ^"..."
+ | "(" Word ^")"
> ^"(" RHS ")" /ws
- | ^"~" e
- > "^^":: "^" e
-
-Word = [a-zA-Z0-9_]++
-Quoted = "\"" ((~[\"\\] | escaped)+) "\""
- | "":: "\"\""
-escaped = "\n":: "\\n"
- | "\r":: "\\r"
- | "\\" ~[nr]
-
-w = " " | "\n" | "\r"
-ws = "()":: w**
- | "()":: w** "//" (~[\n])* "\n" ws
-wp = w++
+ | "~":: ("~" -> ~"~")! e
+ | ^"\\{"
+ | ^"\\}"
+ | ^">>"
+ | ^"<<"
+
+Word:: = [.a-zA-Z0-9_]++ &~ "."+
+Quoted:: = "\"" (~[\"\\] | escaped)+ "\""
+ | "\"\""
+
+Range:: = ec
+ | ec "-" ec
+ | "<<":: [<][<]
+ | ">>":: [>][>]
+
+ec = ~[\-\]\\<>]
+ | [>] -> ~[>]
+ | [<] -> ~[<]
+ | escaped
+escaped = "\n":: "\\n"
+ | "\r":: "\\r"
+ | "\\" ~[nr]
+
+ws! = [ \r\n]**
+ | [ \r\n]** "//" ~[\n]* "\n" ws