From 666244c83ca89846209c6ae3ad5917b900423c8d Mon Sep 17 00:00:00 2001 From: hhsprings Date: Thu, 5 Nov 2015 18:12:54 +0900 Subject: Add the lexer for original BNF. --- pygments/lexers/grammar_notation.py | 57 ++++++++++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 7 deletions(-) (limited to 'pygments/lexers/grammar_notation.py') diff --git a/pygments/lexers/grammar_notation.py b/pygments/lexers/grammar_notation.py index 65475646..43171387 100644 --- a/pygments/lexers/grammar_notation.py +++ b/pygments/lexers/grammar_notation.py @@ -9,17 +9,60 @@ :license: BSD, see LICENSE for details. """ -import re +from pygments.lexer import RegexLexer, bygroups +from pygments.token import Punctuation, Text, Comment, Operator, \ + Keyword, Name, Literal -from pygments.lexer import RegexLexer, DelegatingLexer, \ - include, bygroups, using, words -from pygments.token import Punctuation, Other, Text, Comment, Operator, \ - Keyword, Name, String, Number, Whitespace, Literal +__all__ = ['BnfLexer', 'AbnfLexer'] -__all__ = ['AbnfLexer'] +class BnfLexer(RegexLexer): + """ + This lexer is for grammer notations which are similar to + original BNF. + + In order to maximize a number of targets of this lexer, + let's decide some designs: + + * We don't distinct `Terminal Symbol`. + + * We do assume that `NonTerminal Symbol` are always enclosed + with arrow brackets. + + * We do assume that `NonTerminal Symbol` may include + any printable characters except arrow brackets and + space (no `spaces`, just space, i.e., ASCII \x020). + This assumption is for `RBNF `_. + + * We do assume that target notation doesn't support comment. + + * We don't distinct any operators and punctuation except + `::=`. + + Though these desision making might cause too minimal highlighting + and you might be disappointed, but it is reasonable for us. + + .. versionadded:: 2.1 + """ + + name = 'BNF' + aliases = ['bnf'] + filenames = ['*.bnf'] + mimetypes = ['text/x-bnf'] + + tokens = { + 'root': [ + (r'(<)([ -;=?-~]+)(>)', + bygroups(Punctuation, Name.Class, Punctuation)), + + # an only operator + (r'::=', Operator), + + # fallback + (r'.', Text), + ], + } -# EBNF shold be moved here, i think. class AbnfLexer(RegexLexer): """ -- cgit v1.2.1