Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update nimrod.py lexer #1970

Merged
merged 23 commits into from Sep 24, 2022
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -45,10 +45,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 @@ -68,40 +68,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, Keyword, Text), 'casebranch'),
matkuki marked this conversation as resolved.
Show resolved Hide resolved

# 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\s|func\s|method\s|macro\s|template\s)(?![(\[\]])',
Keyword, 'funcname'),
matkuki marked this conversation as resolved.
Show resolved Hide resolved
(r'(%s)\b' % underscorize(keywords), Keyword),
(r'(%s)\b' % underscorize(['from', 'import', 'include']),
Keyword.Namespace),
(r'(v_?a_?r)\b', Keyword.Declaration),
(r'(%s)\b' % underscorize(types), Keyword.Type),
(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 @@ -118,6 +133,18 @@ def underscorize(words):
(r'\$', String)
# newlines are an error (use "nl" state)
],
'doccomment': [
(r'[^\]#]', String.Doc),
matkuki marked this conversation as resolved.
Show resolved Hide resolved
(r'##\[', String.Doc, '#push'),
(r'\]##', String.Doc, '#pop'),
(r'[\]#]', String.Doc),
],
'comment': [
(r'[^\]#]', Comment.Multiline),
matkuki marked this conversation as resolved.
Show resolved Hide resolved
(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 @@ -130,9 +157,9 @@ def underscorize(words):
include('strings')
],
'tdqs': [
(r'"""(?!")', String, '#pop'),
(r'"""', String.Double, '#pop'),
include('strings'),
include('nl')
(r'\n', String.Double)
matkuki marked this conversation as resolved.
Show resolved Hide resolved
],
'funcname': [
(r'((?![\d_])\w)(((?!_)\w)|(_(?!_)\w))*', Name.Function, '#pop'),
Expand All @@ -142,7 +169,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 @@ -155,4 +182,16 @@ def underscorize(words):
(r'\'i(8|16)', Number.Integer),
default('#pop')
],
'casebranch': [
(r',', Text),
matkuki marked this conversation as resolved.
Show resolved Hide resolved
(r'[\n ]+', Text.Whitespace),
(r':', Operator, '#pop'),
(r'\w+|\W+|[^:]', Name.Label),
],
'pragma': [
(r'[:,]', Text),
matkuki marked this conversation as resolved.
Show resolved Hide resolved
(r'[\n ]+', Text.Whitespace),
(r'\.\}', String.Other, '#pop'),
(r'\w+|\W+|[^.}]', String.Other),
],
}