3 pygments.lexers.grammar_notation |
3 pygments.lexers.grammar_notation |
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
5 |
5 |
6 Lexers for grammer notations like BNF. |
6 Lexers for grammer notations like BNF. |
7 |
7 |
8 :copyright: Copyright 2006-2017 by the Pygments team, see AUTHORS. |
8 :copyright: Copyright 2006-2019 by the Pygments team, see AUTHORS. |
9 :license: BSD, see LICENSE for details. |
9 :license: BSD, see LICENSE for details. |
10 """ |
10 """ |
11 |
11 |
12 import re |
12 import re |
13 |
13 |
14 from pygments.lexer import RegexLexer, bygroups, include, this, using, words |
14 from pygments.lexer import RegexLexer, bygroups, include, this, using, words |
15 from pygments.token import Comment, Keyword, Literal, Name, Number, \ |
15 from pygments.token import Comment, Keyword, Literal, Name, Number, \ |
16 Operator, Punctuation, String, Text |
16 Operator, Punctuation, String, Text |
17 |
17 |
18 __all__ = ['BnfLexer', 'AbnfLexer', 'JsgfLexer'] |
18 __all__ = ['BnfLexer', 'AbnfLexer', 'JsgfLexer', 'PegLexer'] |
19 |
19 |
20 |
20 |
21 class BnfLexer(RegexLexer): |
21 class BnfLexer(RegexLexer): |
22 """ |
22 """ |
23 This lexer is for grammer notations which are similar to |
23 This lexer is for grammer notations which are similar to |
209 (r'\n\s*\*', Comment.Multiline), |
209 (r'\n\s*\*', Comment.Multiline), |
210 include('non-comments'), |
210 include('non-comments'), |
211 (r'.', Comment.Multiline), |
211 (r'.', Comment.Multiline), |
212 ], |
212 ], |
213 } |
213 } |
|
214 |
|
215 |
|
216 class PegLexer(RegexLexer): |
|
217 """ |
|
218 This lexer is for `Parsing Expression Grammars |
|
219 <https://bford.info/pub/lang/peg.pdf>`_ (PEG). |
|
220 |
|
221 Various implementations of PEG have made different decisions |
|
222 regarding the syntax, so let's try to be accommodating: |
|
223 |
|
224 * `<-`, `←`, `:`, and `=` are all accepted as rule operators. |
|
225 |
|
226 * Both `|` and `/` are choice operators. |
|
227 |
|
228 * `^`, `↑`, and `~` are cut operators. |
|
229 |
|
230 * A single `a-z` character immediately before a string, or |
|
231 multiple `a-z` characters following a string, are part of the |
|
232 string (e.g., `r"..."` or `"..."ilmsuxa`). |
|
233 |
|
234 .. versionadded:: 2.6 |
|
235 """ |
|
236 |
|
237 name = 'PEG' |
|
238 aliases = ['peg'] |
|
239 filenames = ['*.peg'] |
|
240 mimetypes = ['text/x-peg'] |
|
241 |
|
242 tokens = { |
|
243 'root': [ |
|
244 # Comments |
|
245 (r'#.*', Comment.Single), |
|
246 |
|
247 # All operators |
|
248 (r'<-|[←:=/|&!?*+^↑~]', Operator), |
|
249 |
|
250 # Other punctuation |
|
251 (r'[()]', Punctuation), |
|
252 |
|
253 # Keywords |
|
254 (r'\.', Keyword), |
|
255 |
|
256 # Character classes |
|
257 (r'(\[)([^\]]*(?:\\.[^\]\\]*)*)(\])', |
|
258 bygroups(Punctuation, String, Punctuation)), |
|
259 |
|
260 # Single and double quoted strings (with optional modifiers) |
|
261 (r'[a-z]?"[^"\\]*(?:\\.[^"\\]*)*"[a-z]*', String.Double), |
|
262 (r"[a-z]?'[^'\\]*(?:\\.[^'\\]*)*'[a-z]*", String.Single), |
|
263 |
|
264 # Nonterminals are not whitespace, operators, or punctuation |
|
265 (r'[^\s<←:=/|&!?*+\^↑~()\[\]"\'#]+', Name.Class), |
|
266 |
|
267 # Fallback |
|
268 (r'.', Text), |
|
269 ], |
|
270 } |