Skip to content

Commit

Permalink
Added support for ANTLR4 (#2063)
Browse files Browse the repository at this point in the history
This adds support for ANTLR4.

Languages embedded via actions are not highlighted.
  • Loading branch information
RunDevelopment authored Oct 16, 2019
1 parent 7db0cab commit aaaa29a
Show file tree
Hide file tree
Showing 18 changed files with 572 additions and 3 deletions.
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions components.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@
"title": "Ada",
"owner": "Lucretia"
},
"antlr4": {
"title": "ANTLR4",
"alias": "g4",
"owner": "RunDevelopment"
},
"apacheconf": {
"title": "Apache Configuration",
"owner": "GuiTeK"
Expand Down
65 changes: 65 additions & 0 deletions components/prism-antlr4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
Prism.languages.antlr4 = {
'comment': /\/\/.*|\/\*[\s\S]*?(?:\*\/|$)/,
'string': {
pattern: /'(?:\\.|[^\\'\r\n])*'/,
greedy: true
},
'character-class': {
pattern: /\[(?:\\.|[^\\\]\r\n])*\]/,
greedy: true,
alias: 'regex',
inside: {
'range': {
pattern: /([^[]|(?:^|[^\\])(?:\\\\)*\\\[)-(?!\])/,
lookbehind: true,
alias: 'punctuation'
},
'escape': /\\(?:u(?:[a-fA-F\d]{4}|\{[a-fA-F\d]+\})|[pP]\{[=\w-]+\}|[^\r\nupP])/,
'punctuation': /[\[\]]/
}
},
'action': {
pattern: /\{(?:[^{}]|\{(?:[^{}]|\{(?:[^{}]|\{[^{}]*\})*\})*\})*\}/,
greedy: true,
inside: {
'content': {
// this might be C, C++, Python, Java, C#, or any other language ANTLR4 compiles to
pattern: /(\{)[\s\S]+(?=\})/,
lookbehind: true
},
'punctuation': /[{}]/
}
},
'command': {
pattern: /(->\s*)(?:\s*(?:,\s*)?[a-z]\w*(?:\s*\([^()\r\n]*\))?)+(?=\s*;)/i,
lookbehind: true,
inside: {
'function': /\b\w+(?=\s*(?:[,(]|$))/,
'punctuation': /[,()]/
}
},
'annotation': {
pattern: /@\w+(?:::\w+)*/,
alias: 'keyword'
},
'label': {
pattern: /#[ \t]*\w+/,
alias: 'punctuation'
},
'keyword': /\b(?:catch|channels|finally|fragment|grammar|import|lexer|locals|mode|options|parser|returns|throws|tokens)\b/,
'definition': [
{
pattern: /\b[a-z]\w*(?=\s*:)/,
alias: ['rule', 'class-name']
},
{
pattern: /\b[A-Z]\w*(?=\s*:)/,
alias: ['token', 'constant']
},
],
'constant': /\b[A-Z][A-Z_]*\b/,
'operator': /\.\.|->|[|~]|[*+?]\??/,
'punctuation': /[;:()=]/
};

Prism.languages.g4 = Prism.languages.antlr4;
1 change: 1 addition & 0 deletions components/prism-antlr4.min.js

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

1 change: 1 addition & 0 deletions plugins/autoloader/prism-autoloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
"svg": "markup",
"mathml": "markup",
"js": "javascript",
"g4": "antlr4",
"adoc": "asciidoc",
"shell": "bash",
"rbnf": "bnf",
Expand Down
2 changes: 1 addition & 1 deletion plugins/autoloader/prism-autoloader.min.js

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

2 changes: 2 additions & 0 deletions plugins/show-language/prism-show-language.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"js": "JavaScript",
"abap": "ABAP",
"abnf": "Augmented Backus–Naur form",
"antlr4": "ANTLR4",
"g4": "ANTLR4",
"apacheconf": "Apache Configuration",
"apl": "APL",
"aql": "AQL",
Expand Down
2 changes: 1 addition & 1 deletion plugins/show-language/prism-show-language.min.js

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

49 changes: 49 additions & 0 deletions tests/languages/antlr4/action_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
options { superClass = LexerAdaptor; }

@lexer::header {
import { Token } from 'antlr4ts/Token';
import { CommonToken } from 'antlr4ts/CommonToken';
import { Python3Parser } from './Python3Parser';
}

END : 'end' {System.out.println("found an end");} ;

@header {
from LexerAdaptor import LexerAdaptor
}

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

[
["keyword", "options"],
["action", [
["punctuation", "{"],
["content", " superClass = LexerAdaptor; "],
["punctuation", "}"]
]],
["annotation", "@lexer::header"],
["action", [
["punctuation", "{"],
["content", "\n import { Token } from 'antlr4ts/Token';\n import { CommonToken } from 'antlr4ts/CommonToken';\n import { Python3Parser } from './Python3Parser';\n"],
["punctuation", "}"]
]],
["definition", "END"],
["punctuation", ":"],
["string", "'end'"],
["action", [
["punctuation", "{"],
["content", "System.out.println(\"found an end\");"],
["punctuation", "}"]
]],
["punctuation", ";"],
["annotation", "@header"],
["action", [
["punctuation", "{"],
["content", "\nfrom LexerAdaptor import LexerAdaptor\n"],
["punctuation", "}"]
]]
]

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

Checks for actions.
21 changes: 21 additions & 0 deletions tests/languages/antlr4/annotation_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@header {}
@lexer::header {}

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

[
["annotation", "@header"],
["action", [
["punctuation", "{"],
["punctuation", "}"]
]],
["annotation", "@lexer::header"],
["action", [
["punctuation", "{"],
["punctuation", "}"]
]]
]

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

Checks for annotations.
Loading

0 comments on commit aaaa29a

Please sign in to comment.