source: default/v2/trunk/de.ugoe.cs.swe.bnftools.ebnf/src/de/ugoe/cs/swe/bnftools/EBNF.xtext @ 90

Last change on this file since 90 was 90, checked in by phdmakk, 8 years ago

+ added support for range repetition modifier in grammar

File size: 3.4 KB
Line 
1grammar de.ugoe.cs.swe.bnftools.Ebnf hidden(WS, ML_COMMENT, SL_COMMENT)
2
3import "http://www.eclipse.org/emf/2002/Ecore" as ecore
4
5generate ebnf "http://www.ugoe.de/cs/swe/bnftools/Ebnf"
6
7// -----------------------------------------------------------------------------------------------
8// Parser Rules
9// -----------------------------------------------------------------------------------------------
10EtsiBnf:
11        'grammar' name=ID
12        (       type='/bnf'? ';'
13                (importSection=ImportSection)?
14                (bnfEntry+=BnfEntry)+
15        )
16        |
17        (       type='/delta' ';'
18                (importSection=ImportSection)?
19                (deltaEntry+=DeltaEntry)*
20        )
21        |
22        (       type='/merge' ';'
23                (importSection=ImportSection)?
24                (mergeEntry+=MergeEntry)*
25        )
26;
27
28ImportSection:
29        (imports+=Import)+
30;
31
32BnfEntry:
33        sectionheader=SectionHeading | rule=Rule
34;
35
36DeltaEntry:
37        rule=Rule | sectionheader=SectionHeading | extRule=ExtRule
38;
39
40MergeEntry:
41        sectionheader=SectionHeading | mergeRule=MergeRule
42;
43
44SectionHeading:
45        {SectionHeading}
46        sectionHeader=SECTIONHEADER
47;
48
49
50Import :
51        'import' importURI=STRING
52        ('/' (grammarType='core' | grammarType='package' | grammarType='update'))?
53        ('label:' label=ID)? ';'
54;
55
56//Import: 'import' importedNamespace=FqnWithWildCard;
57//FqnWithWildCard: Fqn('.*')? ';';
58//Fqn:ID('.'ID)*;
59
60
61Rule:
62        (rulenumber=INT (rulevariant=ID)? '.')?  name=ID  '::=' (definitionList=DefinitionList)? ';'?
63;
64
65ExtRule:
66        (rulenumber=INT (rulevariant=ID)? '.')?  name=ID ('(' ruleext=INT ')') '<-'
67        (elements+=Atom | ')' | ']' | '}' | '|' | '(' | '[' | '{' | '*' | '+')* ';'?
68;
69
70MergeRule:
71        GlobalCombinator
72        | RuleCombinator
73        | HookCombinator
74;
75       
76GlobalCombinator:
77('global' 'combinator:') logic=LOGIC ';'?
78;       
79
80RuleCombinator:
81('rule' 'combinator:' name=ID ) logic=LOGIC ('(' LABEL+=STRING ')')* ';'?
82;
83
84HookCombinator:
85'hook' 'combinator:' name=ID '(' ruleext=INT ')' (logic=LOGIC)? ('(' LABEL+=STRING ')')+ ';'?
86;
87
88DefinitionList:
89        singleDefinition+=SingleDefinition ('|' singleDefinition+=SingleDefinition)*
90;
91
92SingleDefinition:
93        (terms+=Term)+
94;
95
96Term:
97        termAtom=Atom
98        | termGroupedSequence=GroupedSequence
99        | termOptionalSequence=OptionalSequence
100        | termRepeatedSequence=RepeatedSequence
101;
102
103Atom:
104        atomStringRule=StringRule
105        | atomRuleReference=RuleReference
106;
107
108RuleReference:
109        ruleref=[Rule]
110;
111
112StringRule:
113        literal=STRING
114        | colon=COLON
115;
116
117GroupedSequence:
118        '(' definitionList+=DefinitionList ')'
119;
120
121OptionalSequence:
122        '[' definitionList+=DefinitionList ']'
123;
124
125RepeatedSequence:
126        '{' definitions+=DefinitionList '}' (morethanonce?='+'? | range=RepeatRange?)
127;
128
129RepeatRange:
130        '#' '(' from=INT ',' to=INT ')'
131;
132
133// -----------------------------------------------------------------------------------------------
134// Lexer Rules
135// -----------------------------------------------------------------------------------------------
136
137terminal ID             : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')* ;
138terminal INT            returns ecore::EInt: ('0'..'9')+;
139terminal WS                     : (' '|'\t'|'\r'? '\n')+;
140terminal COLON          : '"' '"' '"';
141terminal STRING         : '"' !('"')* '"' | "'" !("'")*"'";
142terminal SECTIONHEADER: ('a'..'z'|'A'..'Z') ('.'|('0'..'9'))+ (' '|'\t') !('\n'|'\r')* '\r'? '\n';
143terminal SL_COMMENT     : '//' !('\n'|'\r')* ('\r'? '\n')?;
144terminal ML_COMMENT     : '/*' -> '*/';
145//TODO: a more intuitive notation
146terminal LOGIC          : '/and' | '/or' | '/andr' | '/orr' | '/any' | '/together' ;
Note: See TracBrowser for help on using the repository browser.