Skip to content

Instantly share code, notes, and snippets.

@NobodysNightmare
Created November 7, 2011 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NobodysNightmare/1345283 to your computer and use it in GitHub Desktop.
Save NobodysNightmare/1345283 to your computer and use it in GitHub Desktop.
ANTLR TDL-Grammar
grammar TDL;
specification
: WS? (protocolDefinition | messageDefinition | structDefinition)*;
protocolDefinition
:
'protocol' WS identifier WS? '=' WS? 'ID' WS number
WS? '{'
WS? protocolElement*
'}' WS?
;
protocolElement
: typeDefinition | messageDefinition;
messageDefinition
:
'message' WS identifier WS? '=' WS? (DIGITZEROTOSEVEN | 'ID' WS number)
WS? '{'
WS? field*
'}' WS?
;
typeDefinition
: structDefinition | sequenceDefinition | unionDefinition | forwardDefinition;
structDefinition
: 'struct' WS identifier (WS? '=' WS? 'ID' WS number)?
WS? '{'
WS? field+
'}' WS?
;
sequenceDefinition
: 'sequence' WS '<' WS? type WS? '>' WS? identifier WS? ';' WS?;
unionDefinition
: 'union' WS identifier
WS? '{'
WS? caseDefinition+
'}' WS?
;
caseDefinition
: 'case' WS number WS? ':' WS? type WS identifier WS? ';' WS?;
forwardDefinition
: 'typedef' WS identifier WS? ';' WS?;
field : ('optional' WS)? type WS identifier WS? ';' WS?;
type : primitiveType | identifier | 'any' WS 'defined' WS 'by' WS identifier;
primitiveType
: 'int' | 'string' | 'binary' | 'any';
identifier : letter (letter | DIGIT)*;
number : DIGIT+;
letter : ALPHA | '_';
ALPHA : 'a'..'z'|'A'..'Z';
DIGITZEROTOSEVEN : '0'..'7';
DIGIT : DIGITZEROTOSEVEN|'8'|'9';
WS : ('\t'|' '|'\r'|'\n')+;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment