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-2015 by the Pygments team, see AUTHORS. |
8 :copyright: Copyright 2006-2017 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 from pygments.lexer import RegexLexer, bygroups, words |
12 import re |
13 from pygments.token import Punctuation, Text, Comment, Operator, \ |
13 |
14 Keyword, Name, Literal |
14 from pygments.lexer import RegexLexer, bygroups, include, this, using, words |
15 |
15 from pygments.token import Comment, Keyword, Literal, Name, Number, \ |
16 __all__ = ['BnfLexer', 'AbnfLexer'] |
16 Operator, Punctuation, String, Text |
|
17 |
|
18 __all__ = ['BnfLexer', 'AbnfLexer', 'JsgfLexer'] |
17 |
19 |
18 |
20 |
19 class BnfLexer(RegexLexer): |
21 class BnfLexer(RegexLexer): |
20 """ |
22 """ |
21 This lexer is for grammer notations which are similar to |
23 This lexer is for grammer notations which are similar to |
127 # fallback |
129 # fallback |
128 (r'\s+', Text), |
130 (r'\s+', Text), |
129 (r'.', Text), |
131 (r'.', Text), |
130 ], |
132 ], |
131 } |
133 } |
|
134 |
|
135 |
|
136 class JsgfLexer(RegexLexer): |
|
137 """ |
|
138 For `JSpeech Grammar Format <https://www.w3.org/TR/jsgf/>`_ |
|
139 grammars. |
|
140 |
|
141 .. versionadded:: 2.2 |
|
142 """ |
|
143 name = 'JSGF' |
|
144 aliases = ['jsgf'] |
|
145 filenames = ['*.jsgf'] |
|
146 mimetypes = ['application/jsgf', 'application/x-jsgf', 'text/jsgf'] |
|
147 |
|
148 flags = re.MULTILINE | re.UNICODE |
|
149 |
|
150 tokens = { |
|
151 'root': [ |
|
152 include('comments'), |
|
153 include('non-comments'), |
|
154 ], |
|
155 'comments': [ |
|
156 (r'/\*\*(?!/)', Comment.Multiline, 'documentation comment'), |
|
157 (r'/\*[\w\W]*?\*/', Comment.Multiline), |
|
158 (r'//.*', Comment.Single), |
|
159 ], |
|
160 'non-comments': [ |
|
161 ('\A#JSGF[^;]*', Comment.Preproc), |
|
162 (r'\s+', Text), |
|
163 (r';', Punctuation), |
|
164 (r'[=|()\[\]*+]', Operator), |
|
165 (r'/[^/]+/', Number.Float), |
|
166 (r'"', String.Double, 'string'), |
|
167 (r'\{', String.Other, 'tag'), |
|
168 (words(('import', 'public'), suffix=r'\b'), Keyword.Reserved), |
|
169 (r'grammar\b', Keyword.Reserved, 'grammar name'), |
|
170 (r'(<)(NULL|VOID)(>)', |
|
171 bygroups(Punctuation, Name.Builtin, Punctuation)), |
|
172 (r'<', Punctuation, 'rulename'), |
|
173 (r'\w+|[^\s;=|()\[\]*+/"{<\w]+', Text), |
|
174 ], |
|
175 'string': [ |
|
176 (r'"', String.Double, '#pop'), |
|
177 (r'\\.', String.Escape), |
|
178 (r'[^\\"]+', String.Double), |
|
179 ], |
|
180 'tag': [ |
|
181 (r'\}', String.Other, '#pop'), |
|
182 (r'\\.', String.Escape), |
|
183 (r'[^\\}]+', String.Other), |
|
184 ], |
|
185 'grammar name': [ |
|
186 (r';', Punctuation, '#pop'), |
|
187 (r'\s+', Text), |
|
188 (r'\.', Punctuation), |
|
189 (r'[^;\s.]+', Name.Namespace), |
|
190 ], |
|
191 'rulename': [ |
|
192 (r'>', Punctuation, '#pop'), |
|
193 (r'\*', Punctuation), |
|
194 (r'\s+', Text), |
|
195 (r'([^.>]+)(\s*)(\.)', bygroups(Name.Namespace, Text, Punctuation)), |
|
196 (r'[^.>]+', Name.Constant), |
|
197 ], |
|
198 'documentation comment': [ |
|
199 (r'\*/', Comment.Multiline, '#pop'), |
|
200 (r'(^\s*\*?\s*)(@(?:example|see)\s+)' |
|
201 r'([\w\W]*?(?=(?:^\s*\*?\s*@|\*/)))', |
|
202 bygroups(Comment.Multiline, Comment.Special, |
|
203 using(this, state='example'))), |
|
204 (r'(^\s*\*?\s*)(@\S*)', |
|
205 bygroups(Comment.Multiline, Comment.Special)), |
|
206 (r'[^*\n@]+|\w|\W', Comment.Multiline), |
|
207 ], |
|
208 'example': [ |
|
209 (r'\n\s*\*', Comment.Multiline), |
|
210 include('non-comments'), |
|
211 (r'.', Comment.Multiline), |
|
212 ], |
|
213 } |