Skip to content

Commit

Permalink
[Spice] Add new keywords and fix bugs (#2227)
Browse files Browse the repository at this point in the history
* Cleanup

* Add scope access operator

* Add enum keyword

* Update test ref

* Fix bug for tokenizing number formats

* Add pr to CHANGES
  • Loading branch information
marcauberer committed Sep 13, 2022
1 parent e94cb7e commit ca56bf2
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Expand Up @@ -19,6 +19,8 @@ Version 2.14.0
* Inform 6: fix lexing of properties and doubles (#2214)
* NASM: add support for SSE/AVX/AVX-512 registers as well as 'rel'
and 'abs' address operators (#2212)
* Spice: add ``enum`` keyword and fix a bug regarding binary,
hexadecimal and octal number tokens (#2227)

- Fix `make mapfiles` when Pygments is not installed in editable mode
(#2223)
Expand Down
16 changes: 8 additions & 8 deletions pygments/lexers/spice.py
Expand Up @@ -40,25 +40,25 @@ class SpiceLexer(RegexLexer):
(r'/(\\\n)?[*](.|\n)*?[*](\\\n)?/', Comment.Multiline),
# keywords
(r'(import|as)\b', Keyword.Namespace),
(r'(f|p|type|struct)\b', Keyword.Declaration),
(r'(f|p|type|struct|enum)\b', Keyword.Declaration),
(words(('if', 'else', 'for', 'foreach', 'while', 'break', 'continue', 'return', 'assert', 'thread', 'unsafe', 'ext', 'dll'), suffix=r'\b'), Keyword),
(words(('const', 'signed', 'unsigned', 'inline', 'public'), suffix=r'\b'), Keyword.Pseudo),
(words(('new', 'switch', 'case', 'yield', 'stash', 'pick', 'sync'), suffix=r'\b'), Keyword.Reserved),
(words(('new', 'switch', 'case', 'yield', 'stash', 'pick', 'sync', 'class'), suffix=r'\b'), Keyword.Reserved),
(r'(true|false|nil)\b', Keyword.Constant),
(words(('double', 'int', 'short', 'long', 'byte', 'char', 'string', 'bool', 'dyn'), suffix=r'\b'), Keyword.Type),
(words(('printf', 'sizeof', 'len', 'tid', 'join'), suffix=r'\b(\()'), bygroups(Name.Builtin, Punctuation)),
# numeric literals
(r'([0-9]*[.][0-9]+)', Number.Double),
(r'((0[dD])?[0-9]+[sl]?)', Number.Integer),
(r'(0[bB][01]+[sl]?)', Number.Bin),
(r'(0[oO][0-7]+[sl]?)', Number.Oct),
(r'(0[xXhH][0-9a-fA-F]+[sl]?)', Number.Hex),
(r'[0-9]*[.][0-9]+', Number.Double),
(r'0[bB][01]+[sl]?', Number.Bin),
(r'0[oO][0-7]+[sl]?', Number.Oct),
(r'0[xXhH][0-9a-fA-F]+[sl]?', Number.Hex),
(r'(0[dD])?[0-9]+[sl]?', Number.Integer),
# string literal
(r'"(\\\\|\\[^\\]|[^"\\])*"', String),
# char literal
(r'\'(\\\\|\\[^\\]|[^\'\\])\'', String.Char),
# tokens
(r'(<<=|>>=|<<|>>|<=|>=|\+=|-=|\*=|/=|\%=|\|=|&=|\^=|&&|\|\||&|\||\+\+|--|\%|\^|\~|==|!=|[.]{3}|[+\-*/&])', Operator),
(r'<<=|>>=|<<|>>|<=|>=|\+=|-=|\*=|/=|\%=|\|=|&=|\^=|&&|\|\||&|\||\+\+|--|\%|\^|\~|==|!=|::|[.]{3}|[+\-*/&]', Operator),
(r'[|<>=!()\[\]{}.,;:\?]', Punctuation),
# identifiers
(r'[^\W\d]\w*', Name.Other),
Expand Down
16 changes: 16 additions & 0 deletions tests/examplefiles/spice/example.spice
@@ -1,3 +1,19 @@
const int test 0x123;

type BloodType enum {
A,
B = 3,
AB,
ZERO = 0
}

type Person struct {
string firstName
string lastName
unsigned int age
BloodType bloodType
}

// Function to compute fibonacci numbers recursively
f<int> fib(int n) {
if n <= 2 {
Expand Down
93 changes: 93 additions & 0 deletions tests/examplefiles/spice/example.spice.output

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ca56bf2

Please sign in to comment.