1 | <?php |
||
32 | class Parser extends Stateful |
||
33 | { |
||
34 | public const T_PRAGMA = 'T_PRAGMA'; |
||
35 | public const T_INCLUDE = 'T_INCLUDE'; |
||
36 | public const T_TOKEN = 'T_TOKEN'; |
||
37 | public const T_SKIP = 'T_SKIP'; |
||
38 | public const T_OR = 'T_OR'; |
||
39 | public const T_TOKEN_SKIPPED = 'T_TOKEN_SKIPPED'; |
||
40 | public const T_TOKEN_KEPT = 'T_TOKEN_KEPT'; |
||
41 | public const T_TOKEN_STRING = 'T_TOKEN_STRING'; |
||
42 | public const T_INVOKE = 'T_INVOKE'; |
||
43 | public const T_GROUP_OPEN = 'T_GROUP_OPEN'; |
||
44 | public const T_GROUP_CLOSE = 'T_GROUP_CLOSE'; |
||
45 | public const T_REPEAT_ZERO_OR_ONE = 'T_REPEAT_ZERO_OR_ONE'; |
||
46 | public const T_REPEAT_ONE_OR_MORE = 'T_REPEAT_ONE_OR_MORE'; |
||
47 | public const T_REPEAT_ZERO_OR_MORE = 'T_REPEAT_ZERO_OR_MORE'; |
||
48 | public const T_REPEAT_N_TO_M = 'T_REPEAT_N_TO_M'; |
||
49 | public const T_REPEAT_N_OR_MORE = 'T_REPEAT_N_OR_MORE'; |
||
50 | public const T_REPEAT_ZERO_TO_M = 'T_REPEAT_ZERO_TO_M'; |
||
51 | public const T_REPEAT_EXACTLY_N = 'T_REPEAT_EXACTLY_N'; |
||
52 | public const T_KEPT_NAME = 'T_KEPT_NAME'; |
||
53 | public const T_NAME = 'T_NAME'; |
||
54 | public const T_EQ = 'T_EQ'; |
||
55 | public const T_DELEGATE = 'T_DELEGATE'; |
||
56 | public const T_END_OF_RULE = 'T_END_OF_RULE'; |
||
57 | public const T_WHITESPACE = 'T_WHITESPACE'; |
||
58 | public const T_COMMENT = 'T_COMMENT'; |
||
59 | public const T_BLOCK_COMMENT = 'T_BLOCK_COMMENT'; |
||
60 | |||
61 | /** |
||
62 | * Lexical tokens list. |
||
63 | * |
||
64 | * @var string[] |
||
65 | */ |
||
66 | protected const LEXER_TOKENS = [ |
||
67 | self::T_PRAGMA => '%pragma\\h+([\\w\\.]+)\\h+([^\\s]+)', |
||
68 | self::T_INCLUDE => '%include\\h+([^\\s]+)', |
||
69 | self::T_TOKEN => '%token\\h+(\\w+)\\h+([^\\s]+)', |
||
70 | self::T_SKIP => '%skip\\h+(\\w+)\\h+([^\\s]+)', |
||
71 | self::T_OR => '\\|', |
||
72 | self::T_TOKEN_SKIPPED => '::(\\w+)::', |
||
73 | self::T_TOKEN_KEPT => '<(\\w+)>', |
||
74 | self::T_TOKEN_STRING => '("[^"\\\\]+(\\\\.[^"\\\\]*)*"|\'[^\'\\\\]+(\\\\.[^\'\\\\]*)*\')', |
||
75 | self::T_INVOKE => '(\\w+)\\(\\)', |
||
76 | self::T_GROUP_OPEN => '\\(', |
||
77 | self::T_GROUP_CLOSE => '\\)', |
||
78 | self::T_REPEAT_ZERO_OR_ONE => '\\?', |
||
79 | self::T_REPEAT_ONE_OR_MORE => '\\+', |
||
80 | self::T_REPEAT_ZERO_OR_MORE => '\\*', |
||
81 | self::T_REPEAT_N_TO_M => '{\\h*(\\-?\\d+)\\h*,\\h*(\\-?\\d+)\\h*}', |
||
82 | self::T_REPEAT_N_OR_MORE => '{\\h*(\\-?\\d+)\\h*,\\h*}', |
||
83 | self::T_REPEAT_ZERO_TO_M => '{\\h*,\\h*(\\-?\\d+)\\h*}', |
||
84 | self::T_REPEAT_EXACTLY_N => '{\\h*(\\-?\\d+)\\h*}', |
||
85 | self::T_KEPT_NAME => '#', |
||
86 | self::T_NAME => '[a-zA-Z_\\x7f-\\xff\\\\][a-zA-Z0-9_\\x7f-\\xff\\\\]*', |
||
87 | self::T_EQ => '(\\:|\\:\\:=|=)', |
||
88 | self::T_DELEGATE => '\\->', |
||
89 | self::T_END_OF_RULE => ';', |
||
90 | self::T_WHITESPACE => '(\\xfe\\xff|\\x20|\\x09|\\x0a|\\x0d)+', |
||
91 | self::T_COMMENT => '//[^\\n]*', |
||
92 | self::T_BLOCK_COMMENT => '/\\*.*?\\*/', |
||
93 | ]; |
||
94 | |||
95 | /** |
||
96 | * List of skipped tokens. |
||
97 | * |
||
98 | * @var string[] |
||
99 | */ |
||
100 | protected const LEXER_SKIPPED_TOKENS = [ |
||
101 | 'T_WHITESPACE', |
||
102 | 'T_COMMENT', |
||
103 | 'T_BLOCK_COMMENT', |
||
104 | ]; |
||
105 | |||
106 | /** |
||
107 | * @var int |
||
108 | */ |
||
109 | protected const LEXER_FLAGS = Factory::LOOKAHEAD; |
||
110 | |||
111 | /** |
||
112 | * List of rule delegates. |
||
113 | * |
||
114 | * @var string[] |
||
115 | */ |
||
116 | protected const PARSER_DELEGATES = [ |
||
117 | 'TokenDefinition' => \Railt\Compiler\Grammar\Delegate\TokenDelegate::class, |
||
118 | 'IncludeDefinition' => \Railt\Compiler\Grammar\Delegate\IncludeDelegate::class, |
||
119 | 'RuleDefinition' => \Railt\Compiler\Grammar\Delegate\RuleDelegate::class, |
||
120 | ]; |
||
121 | |||
122 | /** |
||
123 | * Parser root rule name. |
||
124 | * |
||
125 | * @var string |
||
126 | */ |
||
127 | protected const PARSER_ROOT_RULE = 'Grammar'; |
||
128 | |||
129 | /** |
||
130 | * @return ParserInterface |
||
131 | * @throws \InvalidArgumentException |
||
132 | * @throws \Railt\Lexer\Exception\BadLexemeException |
||
133 | */ |
||
134 | protected function boot(): ParserInterface |
||
138 | |||
139 | /** |
||
140 | * @return LexerInterface |
||
141 | * @throws \InvalidArgumentException |
||
142 | * @throws \Railt\Lexer\Exception\BadLexemeException |
||
143 | */ |
||
144 | protected function bootLexer(): LexerInterface |
||
148 | |||
149 | /** |
||
150 | * @return GrammarInterface |
||
151 | */ |
||
152 | protected function bootGrammar(): GrammarInterface |
||
228 | } |
||
229 |