Skip to content

Commit

Permalink
Update nimrod.py lexer (#1970)
Browse files Browse the repository at this point in the history
Co-authored-by: Jean Abou-Samra <jean@abou-samra.fr>
  • Loading branch information
matkuki and jeanas committed Sep 24, 2022
1 parent fccb16b commit 202426c
Show file tree
Hide file tree
Showing 4 changed files with 2,696 additions and 2,670 deletions.
65 changes: 52 additions & 13 deletions pygments/lexers/nimrod.py
Expand Up @@ -10,7 +10,7 @@

import re

from pygments.lexer import RegexLexer, include, default
from pygments.lexer import RegexLexer, include, default, bygroups
from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
Number, Punctuation, Error

Expand Down Expand Up @@ -47,10 +47,10 @@ def underscorize(words):
'addr', 'and', 'as', 'asm', 'bind', 'block', 'break', 'case',
'cast', 'concept', 'const', 'continue', 'converter', 'defer', 'discard',
'distinct', 'div', 'do', 'elif', 'else', 'end', 'enum', 'except',
'export', 'finally', 'for', 'func', 'if', 'in', 'yield', 'interface',
'is', 'isnot', 'iterator', 'let', 'macro', 'method', 'mixin', 'mod',
'not', 'notin', 'object', 'of', 'or', 'out', 'proc', 'ptr', 'raise',
'ref', 'return', 'shl', 'shr', 'static', 'template', 'try',
'export', 'finally', 'for', 'if', 'in', 'yield', 'interface',
'is', 'isnot', 'iterator', 'let', 'mixin', 'mod',
'not', 'notin', 'object', 'of', 'or', 'out', 'ptr', 'raise',
'ref', 'return', 'shl', 'shr', 'static', 'try',
'tuple', 'type', 'using', 'when', 'while', 'xor'
]

Expand All @@ -70,40 +70,55 @@ def underscorize(words):

tokens = {
'root': [
# Comments
(r'##\[', String.Doc, 'doccomment'),
(r'##.*$', String.Doc),
(r'#\[', Comment.Multiline, 'comment'),
(r'#.*$', Comment),

# Pragmas
(r'\{\.', String.Other, 'pragma'),

# Operators
(r'[*=><+\-/@$~&%!?|\\\[\]]', Operator),
(r'\.\.|\.|,|\[\.|\.\]|\{\.|\.\}|\(\.|\.\)|\{|\}|\(|\)|:|\^|`|;',
Punctuation),


# Case statement branch
(r'(\n\s*)(of)(\s)', bygroups(Text.Whitespace, Keyword, Text.Whitespace), 'casebranch'),

# Strings
(r'(?:[\w]+)"', String, 'rdqs'),
(r'"""', String, 'tdqs'),
(r'"""', String.Double, 'tdqs'),
('"', String, 'dqs'),

# Char
("'", String.Char, 'chars'),

# Keywords
(r'(%s)\b' % underscorize(opWords), Operator.Word),
(r'(p_?r_?o_?c_?\s)(?![(\[\]])', Keyword, 'funcname'),
(r'(proc|func|method|macro|template)(\s)(?![(\[\]])',
bygroups(Keyword, Text.Whitespace), 'funcname'),
(r'(%s)\b' % underscorize(keywords), Keyword),
(r'(%s)\b' % underscorize(['from', 'import', 'include', 'export']),
Keyword.Namespace),
(r'(v_?a_?r)\b', Keyword.Declaration),
(r'(%s)\b' % underscorize(types), Name.Builtin),
(r'(%s)\b' % underscorize(keywordsPseudo), Keyword.Pseudo),

# Identifiers
(r'\b((?![_\d])\w)(((?!_)\w)|(_(?!_)\w))*', Name),

# Numbers
(r'[0-9][0-9_]*(?=([e.]|\'f(32|64)))',
Number.Float, ('float-suffix', 'float-number')),
(r'0x[a-f0-9][a-f0-9_]*', Number.Hex, 'int-suffix'),
(r'0b[01][01_]*', Number.Bin, 'int-suffix'),
(r'0o[0-7][0-7_]*', Number.Oct, 'int-suffix'),
(r'[0-9][0-9_]*', Number.Integer, 'int-suffix'),

# Whitespace
(r'\s+', Text),
(r'\s+', Text.Whitespace),
(r'.+$', Error),
],
'chars': [
Expand All @@ -120,6 +135,18 @@ def underscorize(words):
(r'\$', String)
# newlines are an error (use "nl" state)
],
'doccomment': [
(r'[^\]#]+', String.Doc),
(r'##\[', String.Doc, '#push'),
(r'\]##', String.Doc, '#pop'),
(r'[\]#]', String.Doc),
],
'comment': [
(r'[^\]#]+', Comment.Multiline),
(r'#\[', Comment.Multiline, '#push'),
(r'\]#', Comment.Multiline, '#pop'),
(r'[\]#]', Comment.Multiline),
],
'dqs': [
(r'\\([\\abcefnrtvl"\']|\n|x[a-f0-9]{2}|[0-9]{1,3})',
String.Escape),
Expand All @@ -132,9 +159,9 @@ def underscorize(words):
include('strings')
],
'tdqs': [
(r'"""(?!")', String, '#pop'),
(r'"""', String.Double, '#pop'),
include('strings'),
include('nl')
(r'\n', String.Double)
],
'funcname': [
(r'((?![\d_])\w)(((?!_)\w)|(_(?!_)\w))*', Name.Function, '#pop'),
Expand All @@ -144,7 +171,7 @@ def underscorize(words):
(r'\n', String)
],
'float-number': [
(r'\.(?!\.)[0-9_]*', Number.Float),
(r'\.(?!\.)[0-9_]*[f]*', Number.Float),
(r'e[+-]?[0-9][0-9_]*', Number.Float),
default('#pop')
],
Expand All @@ -157,4 +184,16 @@ def underscorize(words):
(r'\'i(8|16)', Number.Integer),
default('#pop')
],
'casebranch': [
(r',', Punctuation),
(r'[\n ]+', Text.Whitespace),
(r':', Operator, '#pop'),
(r'\w+|[^:]', Name.Label),
],
'pragma': [
(r'[:,]', Text),
(r'[\n ]+', Text.Whitespace),
(r'\.\}', String.Other, '#pop'),
(r'\w+|\W+|[^.}]', String.Other),
],
}

0 comments on commit 202426c

Please sign in to comment.