Skip to content

Commit

Permalink
Haxe: Improved tokenization (#3211)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment authored Dec 5, 2021
1 parent f11b86e commit f41bcf2
Show file tree
Hide file tree
Showing 16 changed files with 772 additions and 152 deletions.
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions components.json
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@
"haxe": {
"title": "Haxe",
"require": "clike",
"optional": "regex",
"owner": "Golmote"
},
"hcl": {
Expand Down
75 changes: 54 additions & 21 deletions components/prism-haxe.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,78 @@
Prism.languages.haxe = Prism.languages.extend('clike', {
// Strings can be multi-line
'string': {
pattern: /(["'])(?:(?!\1)[^\\]|\\[\s\S])*\1/,
// Strings can be multi-line
pattern: /"(?:[^"\\]|\\[\s\S])*"/,
greedy: true
},
'class-name': [
{
pattern: /(\b(?:abstract|class|enum|extends|implements|interface|new|typedef)\s+)[A-Z_]\w*/,
lookbehind: true,
},
// based on naming convention
/\b[A-Z]\w*/
],
// The final look-ahead prevents highlighting of keywords if expressions such as "haxe.macro.Expr"
'keyword': /\bthis\b|\b(?:abstract|as|break|case|cast|catch|class|continue|default|do|dynamic|else|enum|extends|extern|final|for|from|function|if|implements|import|in|inline|interface|macro|new|null|operator|overload|override|package|private|public|return|static|super|switch|throw|to|try|typedef|untyped|using|var|while)(?!\.)\b/,
'function': {
pattern: /\b[a-z_]\w*(?=\s*(?:<[^<>]*>\s*)?\()/i,
greedy: true
},
'operator': /\.{3}|\+\+|--|&&|\|\||->|=>|(?:<<?|>{1,3}|[-+*/%!=&|^])=?|[?:~]/
});

Prism.languages.insertBefore('haxe', 'string', {
'string-interpolation': {
pattern: /'(?:[^'\\]|\\[\s\S])*'/,
greedy: true,
inside: {
'interpolation': {
pattern: /(^|[^\\])\$(?:\w+|\{[^}]+\})/,
pattern: /(^|[^\\])\$(?:\w+|\{[^{}]+\})/,
lookbehind: true,
inside: {
'interpolation': {
pattern: /^\$\w*/,
alias: 'variable'
}
// See rest below
'interpolation-punctuation': {
pattern: /^\$\{?|\}$/,
alias: 'punctuation'
},
'expression': {
pattern: /[\s\S]+/,
inside: Prism.languages.haxe
},
}
}
},
'string': /[\s\S]+/
}
},
// The final look-ahead prevents highlighting of keywords if expressions such as "haxe.macro.Expr"
'keyword': /\bthis\b|\b(?:abstract|as|break|case|cast|catch|class|continue|default|do|dynamic|else|enum|extends|extern|for|from|function|if|implements|import|in|inline|interface|macro|new|null|override|private|public|return|static|super|switch|throw|to|try|typedef|using|var|while)(?!\.)\b/,
'operator': /\.{3}|\+\+?|-[->]?|[=!]=?|&&?|\|\|?|<[<=]?|>[>=]?|[*\/%~^]/
}
});

Prism.languages.insertBefore('haxe', 'class-name', {
'regex': {
pattern: /~\/(?:[^\/\\\r\n]|\\.)+\/[igmsu]*/,
greedy: true
pattern: /~\/(?:[^\/\\\r\n]|\\.)+\/[a-z]*/,
greedy: true,
inside: {
'regex-flags': /\b[a-z]+$/,
'regex-source': {
pattern: /^(~\/)[\s\S]+(?=\/$)/,
lookbehind: true,
alias: 'language-regex',
inside: Prism.languages.regex
},
'regex-delimiter': /^~\/|\/$/,
}
}
});

Prism.languages.insertBefore('haxe', 'keyword', {
'preprocessor': {
pattern: /#\w+/,
alias: 'builtin'
pattern: /#(?:else|elseif|end|if)\b.*/,
alias: 'property'
},
'metadata': {
pattern: /@:?\w+/,
pattern: /@:?[\w.]+/,
alias: 'symbol'
},
'reification': {
pattern: /\$(?:\w+|(?=\{))/,
alias: 'variable'
alias: 'important'
}
});
Prism.languages.haxe['string'].inside['interpolation'].inside.rest = Prism.languages.haxe;
delete Prism.languages.haxe['class-name'];
2 changes: 1 addition & 1 deletion components/prism-haxe.min.js

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

99 changes: 99 additions & 0 deletions tests/languages/haxe!+regex/regex_inclusion.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
~/ha\/xe/i
~/[A-Z0-9._%-]+@[A-Z0-9.-]+.[A-Z][A-Z][A-Z]?/i
~/(dog|fox)/igmsu

----------------------------------------------------

[
["regex", [
["regex-delimiter", "~/"],
["regex-source", [
"ha",
["escape", "\\/"],
"xe"
]],
["regex-delimiter", "/"],
["regex-flags", "i"]
]],
["regex", [
["regex-delimiter", "~/"],
["regex-source", [
["char-class", [
["char-class-punctuation", "["],
["range", [
"A",
["range-punctuation", "-"],
"Z"
]],
["range", [
"0",
["range-punctuation", "-"],
"9"
]],
"._%-",
["char-class-punctuation", "]"]
]],
["quantifier", "+"],
"@",
["char-class", [
["char-class-punctuation", "["],
["range", [
"A",
["range-punctuation", "-"],
"Z"
]],
["range", [
"0",
["range-punctuation", "-"],
"9"
]],
".-",
["char-class-punctuation", "]"]
]],
["quantifier", "+"],
["char-set", "."],
["char-class", [
["char-class-punctuation", "["],
["range", [
"A",
["range-punctuation", "-"],
"Z"
]],
["char-class-punctuation", "]"]
]],
["char-class", [
["char-class-punctuation", "["],
["range", [
"A",
["range-punctuation", "-"],
"Z"
]],
["char-class-punctuation", "]"]
]],
["char-class", [
["char-class-punctuation", "["],
["range", [
"A",
["range-punctuation", "-"],
"Z"
]],
["char-class-punctuation", "]"]
]],
["quantifier", "?"]
]],
["regex-delimiter", "/"],
["regex-flags", "i"]
]],
["regex", [
["regex-delimiter", "~/"],
["regex-source", [
["group", ["("]],
"dog",
["alternation", "|"],
"fox",
["group", ")"]
]],
["regex-delimiter", "/"],
["regex-flags", "igmsu"]
]]
]
Loading

0 comments on commit f41bcf2

Please sign in to comment.