Skip to content

Instantly share code, notes, and snippets.

@mzgoddard
Last active July 24, 2020 16:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mzgoddard/8109065 to your computer and use it in GitHub Desktop.
Save mzgoddard/8109065 to your computer and use it in GitHub Desktop.
TOML Grammers
# A TOML example. Unlike JSON it supports comments.
[player]
particles = [
[ 0.0, -5.0 ],
[ 4.330127018922194, -2.4999999999999996 ],
[ -4.330127018922194, -2.4999999999999996 ]
]
# Unlike YAML, there is only one way to write arrays.
# There is only one way to write tables too.
# This reduces implementation complexity for a TOML parser.
# It also reduces how hard TOML is to use.
size = 10
maxOxygen = 2048
maxResource = 256
grammar toml;
options {
language = C;
output = AST;
ASTLabelType = pANTLR3_BASE_TREE;
}
file : line* ;
line : ( table_header | array_header | entry | ) ( COMMENT | WS ) ;
table_header : '[' table_id ']' ;
array_header : '[[' table_id ']]' ;
table_id : ID ( '.' ID )* ;
entry : ID WS? '=' WS? value ;
value : array | STRING | NUMBER ;
array : '[' WS? ( array_members | string_members | number_members | ) ']' ;
array_members : array ( ',' WS? array )* ','? WS? ;
string_members : STRING ( ',' WS? STRING )* ','? WS? ;
number_members : NUMBER ( ',' WS? NUMBER )* ','? WS? ;
COMMENT : '#' ~('\n')* '\n' {$channel = HIDDEN;} ;
ID : ( 'a'..'z' | 'A'..'Z' | '0'..'9' | '_' )+ ;
STRING : '"' ( '\\"' | . )* '"' ;
NUMBER : '-'? ( '0'..'9'+ | '0'..'9'* '.' '0'..'9'+ ) ;
WS : (' '|'\r'|'\n'|'\t')+ {$channel = HIDDEN;} ;
%token COMMENT ID STRING NUMBER WS
%%
line : line_2 | line line_2 ;
line_2 : WS line_content WS comment_or_ws ;
line_content : table_header | entry | ;
comment_or_ws : COMMENT | ;
table_header : '[' table_header_2 ']' ;
table_header_2 : '[' table_id ']' | table_id ;
table_id : ID more_table_id ;
more_table_id : more_table_id '.' ID | ;
entry : ID WS '=' WS value ;
value : array | STRING | NUMBER ;
array : '[' WS members ']' ;
members : array_members | string_members | number_members | ;
array_members : array | array_members ',' WS array | array_members ',' WS ;
string_members : STRING | string_members ',' WS STRING | string_members ',' WS ;
number_members : NUMBER | number_members ',' WS NUMBER | number_members ',' WS ;
%%
"#[^\n]*\n" { return COMMENT; }
"[a-zA-Z0-9_]+" { return ID; }
"\"(\\\"|.)\"" { return STRING; }
"-?([0-9]+|[0-9]*.[0-9]+)" { return NUMBER; }
"[ \r\n\t]*" { return WS; }
// Reprint of input file "toml-lemon.lemon".
// Symbols:
// 0 $ 11 TRUE 22 table_id
// 1 EOF 12 FALSE 23 id
// 2 COMMENT 13 DATE 24 value
// 3 LEFT_SQUARE 14 error 25 array
// 4 RIGHT_SQUARE 15 file 26 string
// 5 ID_DOT 16 line 27 number
// 6 EQ 17 line_and_comment 28 boolean
// 7 ID 18 line_content 29 date
// 8 COMMA 19 table_header 30 members
// 9 STRING 20 entry 31 value_members
// 10 NUMBER 21 table_header_2 32 comma
file ::= line EOF.
line ::= line_and_comment.
line ::= line line_and_comment.
line_and_comment ::= line_content.
line_and_comment ::= COMMENT.
line_content ::= table_header.
line_content ::= entry.
table_header ::= LEFT_SQUARE table_header_2 RIGHT_SQUARE.
table_header_2 ::= LEFT_SQUARE table_id RIGHT_SQUARE.
table_header_2 ::= table_id.
table_id ::= table_id ID_DOT id.
table_id ::= id.
entry ::= id EQ value.
id ::= ID.
value ::= array.
value ::= string.
value ::= number.
value ::= boolean.
value ::= date.
array ::= LEFT_SQUARE members RIGHT_SQUARE.
members ::= value_members.
members ::=.
value_members ::= value_members comma value.
value_members ::= value_members comma.
value_members ::= value.
comma ::= COMMA.
string ::= STRING.
number ::= NUMBER.
boolean ::= TRUE.
boolean ::= FALSE.
date ::= DATE.
error ::= EOF error.
table_header ::= LEFT_SQUARE error.
entry ::= id EQ error.
entry ::= id error.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment