@@ -3,17 +3,30 @@ import { EmbeddedActionsParser } from 'chevrotain';
33import { And , LogicalChildren , Or } from './logical' ;
44
55import tokens , {
6- Text , LeftParenthesis , RightParenthesis , QuestionMark , Pipe , PlusSign ,
6+ Backslash ,
7+ escapableTokens ,
8+ LeftParenthesis ,
9+ Pipe ,
10+ PlusSign ,
11+ QuestionMark ,
12+ RightParenthesis ,
13+ Text ,
714} from './tokens' ;
815
916/**
1017 * GRAMMAR
1118 *
1219 * element:
13- * Text | optionalText | optionableGroup
20+ * optionableEscapedToken | optionableText | optionableGroup
1421 *
15- * optionalText:
16- * Text QuestionMark
22+ * optionableEscapedToken:
23+ * Backslash
24+ * (Backslash | LeftParenthesis | Pipe | PlusSign | QuestionMark | RightParenthesis)
25+ * (QuestionMark)?
26+ *
27+ * optionableText:
28+ * Text
29+ * (QuestionMark)?
1730 *
1831 * optionableGroup:
1932 * LeftParenthesis
@@ -32,14 +45,28 @@ export default class Parser extends EmbeddedActionsParser {
3245 this . performSelfAnalysis ( ) ;
3346 }
3447
35- private text = this . RULE ( 'text' , ( ) => this . CONSUME ( Text ) . image ) ;
48+ private optionableEscapedToken = this . RULE ( 'escapedToken' , ( ) => {
49+ this . CONSUME1 ( Backslash ) ;
3650
37- private optionalText = this . RULE ( 'optionalText' , ( ) => {
38- const text : string = this . SUBRULE ( this . text ) ;
51+ const token = this . OR (
52+ escapableTokens . map ( ( escapableToken ) => ( { ALT : ( ) => this . CONSUME2 ( escapableToken ) } ) ) ,
53+ ) ;
54+
55+ if ( this . OPTION ( ( ) => this . CONSUME ( QuestionMark ) ) ) {
56+ return new Or ( token . image , '' ) ;
57+ }
3958
40- this . CONSUME ( QuestionMark ) ;
59+ return token . image ;
60+ } ) ;
61+
62+ private optionableText = this . RULE ( 'optionalText' , ( ) => {
63+ const text : string = this . CONSUME ( Text ) . image ;
64+
65+ if ( this . OPTION ( ( ) => this . CONSUME ( QuestionMark ) ) ) {
66+ return new Or ( text , text . slice ( 0 , - 1 ) ) ;
67+ }
4168
42- return this . ACTION ( ( ) => new Or ( text , text . slice ( 0 , - 1 ) ) ) ;
69+ return text ;
4370 } ) ;
4471
4572 private optionableGroup = this . RULE ( 'optionableGroup' , ( ) => {
@@ -72,8 +99,8 @@ export default class Parser extends EmbeddedActionsParser {
7299 } ) ;
73100
74101 private element = this . RULE ( 'element' , ( ) => this . OR ( [
75- { ALT : ( ) => this . SUBRULE ( this . optionalText ) } ,
76- { ALT : ( ) => this . SUBRULE ( this . text ) } ,
102+ { ALT : ( ) => this . SUBRULE ( this . optionableEscapedToken ) } ,
103+ { ALT : ( ) => this . SUBRULE ( this . optionableText ) } ,
77104 { ALT : ( ) => this . SUBRULE ( this . optionableGroup ) } ,
78105 ] ) ) ;
79106
0 commit comments