Skip to content

Commit

Permalink
Macro token syntax coloring (doesn't work in some cases still) (#448)
Browse files Browse the repository at this point in the history
Fix a few comments in ksp_parser.py
  • Loading branch information
mkruselj authored Mar 31, 2024
1 parent 66f09fa commit 21d280c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
25 changes: 17 additions & 8 deletions KSP.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ file_extensions:

scope: source.ksp

variables:
varchars: '[a-zA-Z0-9_.]'
macrotoken: '\#{1}{{varchars}}+\#{1}'

contexts:
main:
- match: '\{\{'
Expand Down Expand Up @@ -50,6 +54,10 @@ contexts:
comment: Operator
scope: keyword.operator.source.ksp

- match: '{{macrotoken}}'
comment: Macro replacement token
scope: variable.parameter.source.ksp

- match: '(on\s+ui_control(s)?)(\s*\(([$%@?]?[a-zA-Z0-9_#.]+)\))?'
comment: UI callback
captures:
Expand Down Expand Up @@ -161,39 +169,39 @@ contexts:
scope: variable.parameter.function.source.ksp
- include: main

- match: '^\s*(const +)([a-zA-Z_.0-9]+)'
- match: '^\s*(const +)({{varchars}}+)'
comment: Const block definition
scope: constblock.ksp
captures:
0: meta.constblock.source.ksp
1: keyword.other.source.ksp
2: entity.name.type.source.ksp

- match: '^\s*(family +)([a-zA-Z_.0-9]+)'
- match: '^\s*(family +)({{varchars}}+)'
comment: Family definition
scope: family.ksp
captures:
0: meta.family.source.ksp
1: keyword.other.source.ksp
2: entity.name.type.source.ksp

- match: '^\s*(list +)([a-zA-Z_.0-9]+)'
- match: '^\s*(list +)({{varchars}}+)'
comment: List definition
scope: list.ksp
captures:
0: meta.list.source.ksp
1: keyword.other.source.ksp
2: entity.name.type.source.ksp

- match: '^\s*(struct +)([a-zA-Z_.0-9]+)'
- match: '^\s*(struct +)({{varchars}}+)'
comment: Struct definition
scope: struct.ksp
captures:
0: meta.struct.source.ksp
1: keyword.other.source.ksp
2: entity.name.type.source.ksp

- match: '\b(call) +([A-Za-z0-9_.]+)?'
- match: '\b(call) +({{varchars}}+)?'
comment: Function call using 'call'
captures:
1: keyword.other.source.ksp
Expand All @@ -220,7 +228,7 @@ contexts:
ui_wavetable
)\s*
((inst)?pers|read)?\s*
(([a-zA-Z0-9_.]+)?(\#(.+?)\#)?([a-zA-Z0-9_.]+)?)\s*\('
(({{varchars}}+)?(\#(.+?)\#)?({{varchars}}+)?)\s*\('
comment: Variable declaration
captures:
1: keyword.other.source.ksp
Expand Down Expand Up @@ -670,7 +678,7 @@ contexts:
comment: Builtin function with arguments
scope: support.function.source.ksp

- match: '([a-zA-Z_0-9.]*)\s*(\()'
- match: '({{varchars}}*)\s*(\()'
comment: Function call
captures:
1: meta.function-call.source.ksp meta.function-name
Expand All @@ -682,7 +690,7 @@ contexts:
pop: true
- include: main

- match: '(\b|[~?$%!@&])[A-Za-z0-9_.]+|\b\d+[A-Za-z_][A-Za-z0-9_]*\b'
- match: '(\b|[~?$%!@&]){{varchars}}+|\b\d+[A-Za-z_][A-Za-z0-9_]*\b'
comment: Identifier
scope: variable.source.ksp

Expand All @@ -693,3 +701,4 @@ contexts:
- match: "[-+*/]"
comment: Arithmetic Operator
scope: arithmetic.source.ksp

8 changes: 4 additions & 4 deletions compiler/ksp_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,16 @@ def t_ID(t):

if t.value == 'mod': # modulo operator
t.type = 'MOD'
elif t.value.lower().startswith('0x') and hex_number_re1.match(t.value): # hex number, eg. 0x10
elif t.value.lower().startswith('0x') and hex_number_re1.match(t.value): # hexadecimal number, e.g. 0x10
t.type = 'INTEGER'
t.value = int(t.value, 16)
elif t.value.lower().endswith('h') and hex_number_re2.match(t.value): # hex number, eg. 010h
elif t.value.lower().endswith('h') and hex_number_re2.match(t.value): # hexadecimal number, e.g. 010h
t.type = 'INTEGER'
t.value = int(t.value[1:-1], 16)
elif t.value.lower().startswith('b') and lsb_left_bin_re1.match(t.value): # hex number, eg. 010h
elif t.value.lower().startswith('b') and lsb_left_bin_re1.match(t.value): # binary number, LSB first, e.g. b010
t.type = 'INTEGER'
t.value = int(t.value.lower().replace('b','')[::-1], 2)
elif t.value.lower().endswith('b') and lsb_right_bin_re1.match(t.value): # hex number, eg. 010h
elif t.value.lower().endswith('b') and lsb_right_bin_re1.match(t.value): # binary number, LSB last, e.g. 010b
t.type = 'INTEGER'
t.value = int(t.value.lower().replace('b',''), 2)
else:
Expand Down

0 comments on commit 21d280c

Please sign in to comment.