grammar_notation.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. """
  2. pygments.lexers.grammar_notation
  3. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. Lexers for grammar notations like BNF.
  5. :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. from pygments.lexer import RegexLexer, bygroups, include, this, using, words
  9. from pygments.token import Comment, Keyword, Literal, Name, Number, \
  10. Operator, Punctuation, String, Text, Whitespace
  11. __all__ = ['BnfLexer', 'AbnfLexer', 'JsgfLexer', 'PegLexer']
  12. class BnfLexer(RegexLexer):
  13. """
  14. This lexer is for grammar notations which are similar to
  15. original BNF.
  16. In order to maximize a number of targets of this lexer,
  17. let's decide some designs:
  18. * We don't distinguish `Terminal Symbol`.
  19. * We do assume that `NonTerminal Symbol` are always enclosed
  20. with arrow brackets.
  21. * We do assume that `NonTerminal Symbol` may include
  22. any printable characters except arrow brackets and ASCII 0x20.
  23. This assumption is for `RBNF <http://www.rfc-base.org/txt/rfc-5511.txt>`_.
  24. * We do assume that target notation doesn't support comment.
  25. * We don't distinguish any operators and punctuation except
  26. `::=`.
  27. Though these decision making might cause too minimal highlighting
  28. and you might be disappointed, but it is reasonable for us.
  29. .. versionadded:: 2.1
  30. """
  31. name = 'BNF'
  32. aliases = ['bnf']
  33. filenames = ['*.bnf']
  34. mimetypes = ['text/x-bnf']
  35. tokens = {
  36. 'root': [
  37. (r'(<)([ -;=?-~]+)(>)',
  38. bygroups(Punctuation, Name.Class, Punctuation)),
  39. # an only operator
  40. (r'::=', Operator),
  41. # fallback
  42. (r'[^<>:]+', Text), # for performance
  43. (r'.', Text),
  44. ],
  45. }
  46. class AbnfLexer(RegexLexer):
  47. """
  48. Lexer for IETF 7405 ABNF.
  49. (Updates `5234 <http://www.ietf.org/rfc/rfc5234.txt>`_) grammars.
  50. .. versionadded:: 2.1
  51. """
  52. name = 'ABNF'
  53. url = 'http://www.ietf.org/rfc/rfc7405.txt'
  54. aliases = ['abnf']
  55. filenames = ['*.abnf']
  56. mimetypes = ['text/x-abnf']
  57. _core_rules = (
  58. 'ALPHA', 'BIT', 'CHAR', 'CR', 'CRLF', 'CTL', 'DIGIT',
  59. 'DQUOTE', 'HEXDIG', 'HTAB', 'LF', 'LWSP', 'OCTET',
  60. 'SP', 'VCHAR', 'WSP')
  61. tokens = {
  62. 'root': [
  63. # comment
  64. (r';.*$', Comment.Single),
  65. # quoted
  66. # double quote itself in this state, it is as '%x22'.
  67. (r'(%[si])?"[^"]*"', Literal),
  68. # binary (but i have never seen...)
  69. (r'%b[01]+\-[01]+\b', Literal), # range
  70. (r'%b[01]+(\.[01]+)*\b', Literal), # concat
  71. # decimal
  72. (r'%d[0-9]+\-[0-9]+\b', Literal), # range
  73. (r'%d[0-9]+(\.[0-9]+)*\b', Literal), # concat
  74. # hexadecimal
  75. (r'%x[0-9a-fA-F]+\-[0-9a-fA-F]+\b', Literal), # range
  76. (r'%x[0-9a-fA-F]+(\.[0-9a-fA-F]+)*\b', Literal), # concat
  77. # repetition (<a>*<b>element) including nRule
  78. (r'\b[0-9]+\*[0-9]+', Operator),
  79. (r'\b[0-9]+\*', Operator),
  80. (r'\b[0-9]+', Operator),
  81. (r'\*', Operator),
  82. # Strictly speaking, these are not keyword but
  83. # are called `Core Rule'.
  84. (words(_core_rules, suffix=r'\b'), Keyword),
  85. # nonterminals (ALPHA *(ALPHA / DIGIT / "-"))
  86. (r'[a-zA-Z][a-zA-Z0-9-]*\b', Name.Class),
  87. # operators
  88. (r'(=/|=|/)', Operator),
  89. # punctuation
  90. (r'[\[\]()]', Punctuation),
  91. # fallback
  92. (r'\s+', Whitespace),
  93. (r'.', Text),
  94. ],
  95. }
  96. class JsgfLexer(RegexLexer):
  97. """
  98. For JSpeech Grammar Format grammars.
  99. .. versionadded:: 2.2
  100. """
  101. name = 'JSGF'
  102. url = 'https://www.w3.org/TR/jsgf/'
  103. aliases = ['jsgf']
  104. filenames = ['*.jsgf']
  105. mimetypes = ['application/jsgf', 'application/x-jsgf', 'text/jsgf']
  106. tokens = {
  107. 'root': [
  108. include('comments'),
  109. include('non-comments'),
  110. ],
  111. 'comments': [
  112. (r'/\*\*(?!/)', Comment.Multiline, 'documentation comment'),
  113. (r'/\*[\w\W]*?\*/', Comment.Multiline),
  114. (r'//.*$', Comment.Single),
  115. ],
  116. 'non-comments': [
  117. (r'\A#JSGF[^;]*', Comment.Preproc),
  118. (r'\s+', Whitespace),
  119. (r';', Punctuation),
  120. (r'[=|()\[\]*+]', Operator),
  121. (r'/[^/]+/', Number.Float),
  122. (r'"', String.Double, 'string'),
  123. (r'\{', String.Other, 'tag'),
  124. (words(('import', 'public'), suffix=r'\b'), Keyword.Reserved),
  125. (r'grammar\b', Keyword.Reserved, 'grammar name'),
  126. (r'(<)(NULL|VOID)(>)',
  127. bygroups(Punctuation, Name.Builtin, Punctuation)),
  128. (r'<', Punctuation, 'rulename'),
  129. (r'\w+|[^\s;=|()\[\]*+/"{<\w]+', Text),
  130. ],
  131. 'string': [
  132. (r'"', String.Double, '#pop'),
  133. (r'\\.', String.Escape),
  134. (r'[^\\"]+', String.Double),
  135. ],
  136. 'tag': [
  137. (r'\}', String.Other, '#pop'),
  138. (r'\\.', String.Escape),
  139. (r'[^\\}]+', String.Other),
  140. ],
  141. 'grammar name': [
  142. (r';', Punctuation, '#pop'),
  143. (r'\s+', Whitespace),
  144. (r'\.', Punctuation),
  145. (r'[^;\s.]+', Name.Namespace),
  146. ],
  147. 'rulename': [
  148. (r'>', Punctuation, '#pop'),
  149. (r'\*', Punctuation),
  150. (r'\s+', Whitespace),
  151. (r'([^.>]+)(\s*)(\.)', bygroups(Name.Namespace, Text, Punctuation)),
  152. (r'[^.>]+', Name.Constant),
  153. ],
  154. 'documentation comment': [
  155. (r'\*/', Comment.Multiline, '#pop'),
  156. (r'^(\s*)(\*?)(\s*)(@(?:example|see))(\s+)'
  157. r'([\w\W]*?(?=(?:^\s*\*?\s*@|\*/)))',
  158. bygroups(Whitespace, Comment.Multiline, Whitespace, Comment.Special,
  159. Whitespace, using(this, state='example'))),
  160. (r'(^\s*\*?\s*)(@\S*)',
  161. bygroups(Comment.Multiline, Comment.Special)),
  162. (r'[^*\n@]+|\w|\W', Comment.Multiline),
  163. ],
  164. 'example': [
  165. (r'(\n\s*)(\*)', bygroups(Whitespace, Comment.Multiline)),
  166. include('non-comments'),
  167. (r'.', Comment.Multiline),
  168. ],
  169. }
  170. class PegLexer(RegexLexer):
  171. """
  172. This lexer is for Parsing Expression Grammars (PEG).
  173. Various implementations of PEG have made different decisions
  174. regarding the syntax, so let's try to be accommodating:
  175. * `<-`, `←`, `:`, and `=` are all accepted as rule operators.
  176. * Both `|` and `/` are choice operators.
  177. * `^`, `↑`, and `~` are cut operators.
  178. * A single `a-z` character immediately before a string, or
  179. multiple `a-z` characters following a string, are part of the
  180. string (e.g., `r"..."` or `"..."ilmsuxa`).
  181. .. versionadded:: 2.6
  182. """
  183. name = 'PEG'
  184. url = 'https://bford.info/pub/lang/peg.pdf'
  185. aliases = ['peg']
  186. filenames = ['*.peg']
  187. mimetypes = ['text/x-peg']
  188. tokens = {
  189. 'root': [
  190. # Comments
  191. (r'#.*$', Comment.Single),
  192. # All operators
  193. (r'<-|[←:=/|&!?*+^↑~]', Operator),
  194. # Other punctuation
  195. (r'[()]', Punctuation),
  196. # Keywords
  197. (r'\.', Keyword),
  198. # Character classes
  199. (r'(\[)([^\]]*(?:\\.[^\]\\]*)*)(\])',
  200. bygroups(Punctuation, String, Punctuation)),
  201. # Single and double quoted strings (with optional modifiers)
  202. (r'[a-z]?"[^"\\]*(?:\\.[^"\\]*)*"[a-z]*', String.Double),
  203. (r"[a-z]?'[^'\\]*(?:\\.[^'\\]*)*'[a-z]*", String.Single),
  204. # Nonterminals are not whitespace, operators, or punctuation
  205. (r'[^\s<←:=/|&!?*+\^↑~()\[\]"\'#]+', Name.Class),
  206. # Fallback
  207. (r'.', Text),
  208. ],
  209. }