Add hexadecimal numbers to the lexer.

Also decimal and hexadecimal exponential notation for float constants.
This commit is contained in:
Jakob Olesen
2016-01-22 10:31:24 -08:00
parent ca02df9ce2
commit e238df3e7c

View File

@@ -2,9 +2,12 @@
# #
# Pygments lexer for Cretonne. # Pygments lexer for Cretonne.
from pygments.lexer import RegexLexer, bygroups from pygments.lexer import RegexLexer, bygroups, words
from pygments.token import * from pygments.token import *
def keywords(*args):
return words(args, prefix=r'\b', suffix=r'\b')
class CretonneLexer(RegexLexer): class CretonneLexer(RegexLexer):
name = 'Cretonne' name = 'Cretonne'
aliases = ['cton'] aliases = ['cton']
@@ -13,19 +16,30 @@ class CretonneLexer(RegexLexer):
tokens = { tokens = {
'root': [ 'root': [
(r';.*?$', Comment.Single), (r';.*?$', Comment.Single),
(r'\b(function|entry)\b', Keyword), # Strings are in double quotes, support \xx escapes only.
(r'\b(align)\b', Name.Attribute), (r'"([^"\\]+|\\[0-9a-fA-F]{2})*"', String),
# A naked function name following 'function' is also a string.
(r'\b(function)([ \t]+)(\w+)\b', bygroups(Keyword, Whitespace, String.Symbol)),
# Numbers.
(r'[-+]?0[xX][0-9a-fA-F]+', Number.Hex),
(r'[-+]?0[xX][0-9a-fA-F]*\.[0-9a-fA-F]*([pP]\d+)?', Number.Hex),
(r'[-+]?\d+\.\d+([eE]\d+)?', Number.Float),
(r'[-+]?\d+', Number.Integer),
# Reserved words.
(keywords('function', 'entry'), Keyword),
# Known attributes.
(keywords('align'), Name.Attribute),
# Well known value types. # Well known value types.
(r'\b(bool|i\d+|f32|f64)(x\d+)?\b', Keyword.Type), (r'\b(bool|i\d+|f32|f64)(x\d+)?\b', Keyword.Type),
(r'\d+', Number.Integer),
(r'0[xX][0-9a-fA-F]+', Number.Hex),
# v<nn> = value # v<nn> = value
# ss<nn> = stack slot # ss<nn> = stack slot
(r'(v|ss)\d+', Name.Variable), (r'(v|ss)\d+', Name.Variable),
# ebb<nn> = extended basic block # ebb<nn> = extended basic block
(r'(ebb)\d+', Name.Label), (r'(ebb)\d+', Name.Label),
# Match instruction names in context.
(r'(=)( *)([a-z]\w*)', bygroups(Operator, Whitespace, Name.Function)), (r'(=)( *)([a-z]\w*)', bygroups(Operator, Whitespace, Name.Function)),
(r'^( +)([a-z]\w*\b)(?! *[,=])', bygroups(Whitespace, Name.Function)), (r'^( +)([a-z]\w*\b)(?! *[,=])', bygroups(Whitespace, Name.Function)),
# Other names: results and arguments
(r'[a-z]\w*', Name), (r'[a-z]\w*', Name),
(r'->|=|:', Operator), (r'->|=|:', Operator),
(r'[{}(),.]', Punctuation), (r'[{}(),.]', Punctuation),