|
| 1 | +%YAML 1.2 |
| 2 | +--- |
| 3 | +# Forth syntax highlighting for Typst |
| 4 | +# Based on: https://github.com/mitranim/sublime-forth |
| 5 | +# Adapted for Typst's regex engine compatibility |
| 6 | +name: Forth |
| 7 | +file_extensions: [f, fs, fth, 4th, forth] |
| 8 | +scope: source.forth |
| 9 | + |
| 10 | +contexts: |
| 11 | + prototype: |
| 12 | + - include: comments |
| 13 | + |
| 14 | + main: |
| 15 | + - include: strings |
| 16 | + - include: numbers |
| 17 | + - include: definitions |
| 18 | + - include: control-flow |
| 19 | + - include: stack-manipulation |
| 20 | + - include: arithmetic |
| 21 | + - include: memory |
| 22 | + - include: variables-constants |
| 23 | + - include: io |
| 24 | + - include: comparison |
| 25 | + |
| 26 | + comments: |
| 27 | + # Line comment with backslash |
| 28 | + - match: '\\.*$' |
| 29 | + scope: comment.line.forth |
| 30 | + # Block comment |
| 31 | + - match: '\(\s' |
| 32 | + push: |
| 33 | + - meta_scope: comment.block.forth |
| 34 | + - match: '\)' |
| 35 | + pop: true |
| 36 | + |
| 37 | + strings: |
| 38 | + # Print string ." |
| 39 | + - match: '\.\"\s' |
| 40 | + push: |
| 41 | + - meta_scope: string.quoted.double.forth |
| 42 | + - match: '"' |
| 43 | + pop: true |
| 44 | + # String literal s" |
| 45 | + - match: 's\"\s' |
| 46 | + scope: support.function.forth |
| 47 | + push: |
| 48 | + - meta_scope: string.quoted.double.forth |
| 49 | + - match: '"' |
| 50 | + pop: true |
| 51 | + # Character literals |
| 52 | + - match: '\bchar\b' |
| 53 | + scope: keyword.other.forth |
| 54 | + - match: '\bchars\b' |
| 55 | + scope: support.function.memory.forth |
| 56 | + |
| 57 | + numbers: |
| 58 | + # Hexadecimal with $ prefix |
| 59 | + - match: '\$[0-9a-fA-F]+' |
| 60 | + scope: constant.numeric.hex.forth |
| 61 | + # Hexadecimal with 0x prefix |
| 62 | + - match: '\b0x[0-9a-fA-F]+\b' |
| 63 | + scope: constant.numeric.hex.forth |
| 64 | + # Binary with % prefix |
| 65 | + - match: '%[01]+' |
| 66 | + scope: constant.numeric.binary.forth |
| 67 | + # Decimal (negative or positive) |
| 68 | + - match: '-?\b[0-9]+\b' |
| 69 | + scope: constant.numeric.decimal.forth |
| 70 | + |
| 71 | + definitions: |
| 72 | + # Colon definition - word name follows |
| 73 | + - match: ':\s+(\S+)' |
| 74 | + captures: |
| 75 | + 0: keyword.control.definition.forth |
| 76 | + 1: entity.name.function.forth |
| 77 | + # Semicolon definition end |
| 78 | + - match: '\s;\s' |
| 79 | + scope: keyword.control.definition.forth |
| 80 | + - match: '\s;$' |
| 81 | + scope: keyword.control.definition.forth |
| 82 | + # Immediate |
| 83 | + - match: '\bimmediate\b' |
| 84 | + scope: keyword.other.forth |
| 85 | + # Postpone and compile |
| 86 | + - match: '\b(postpone|compile|literal)\b' |
| 87 | + scope: keyword.other.forth |
| 88 | + |
| 89 | + control-flow: |
| 90 | + - match: '\b(if|else|then|endif)\b' |
| 91 | + scope: keyword.control.conditional.forth |
| 92 | + - match: '\b(begin|until|while|repeat|again)\b' |
| 93 | + scope: keyword.control.loop.forth |
| 94 | + - match: '\b(do|loop|leave|unloop)\b' |
| 95 | + scope: keyword.control.loop.forth |
| 96 | + - match: '\b(case|of|endof|endcase)\b' |
| 97 | + scope: keyword.control.switch.forth |
| 98 | + - match: '\b(exit|quit|abort)\b' |
| 99 | + scope: keyword.control.flow.forth |
| 100 | + - match: '\b(recurse|execute|evaluate)\b' |
| 101 | + scope: keyword.control.forth |
| 102 | + |
| 103 | + stack-manipulation: |
| 104 | + - match: '\b(dup|drop|swap|over|rot|nip|tuck|pick|roll|depth)\b' |
| 105 | + scope: support.function.stack.forth |
| 106 | + - match: '\b(2dup|2drop|2swap|2over|2rot)\b' |
| 107 | + scope: support.function.stack.forth |
| 108 | + - match: '\b-rot\b' |
| 109 | + scope: support.function.stack.forth |
| 110 | + |
| 111 | + arithmetic: |
| 112 | + - match: '\b(mod|negate|abs|min|max)\b' |
| 113 | + scope: keyword.operator.arithmetic.forth |
| 114 | + - match: '\b(and|or|xor|invert|lshift|rshift)\b' |
| 115 | + scope: keyword.operator.bitwise.forth |
| 116 | + |
| 117 | + memory: |
| 118 | + - match: '\b(here|allot|cells|align|aligned)\b' |
| 119 | + scope: support.function.memory.forth |
| 120 | + - match: '\b(move|fill|erase|cmove)\b' |
| 121 | + scope: support.function.memory.forth |
| 122 | + |
| 123 | + variables-constants: |
| 124 | + - match: '\b(variable|constant|value|2variable|2constant)\b' |
| 125 | + scope: storage.type.forth |
| 126 | + - match: '\b(create)\b' |
| 127 | + scope: storage.type.forth |
| 128 | + - match: '\bto\b' |
| 129 | + scope: keyword.other.forth |
| 130 | + - match: '\b(true|false)\b' |
| 131 | + scope: constant.language.forth |
| 132 | + |
| 133 | + io: |
| 134 | + - match: '\b(emit|type|cr|space|spaces)\b' |
| 135 | + scope: support.function.io.forth |
| 136 | + - match: '\b(key|accept|expect)\b' |
| 137 | + scope: support.function.io.forth |
| 138 | + - match: '\b(open-file|close-file|read-file|write-file|read-line|write-line)\b' |
| 139 | + scope: support.function.io.forth |
| 140 | + - match: '\b(include|require|included)\b' |
| 141 | + scope: keyword.control.import.forth |
| 142 | + |
| 143 | + comparison: |
| 144 | + - match: '\b(within)\b' |
| 145 | + scope: keyword.operator.comparison.forth |
0 commit comments