foo123 / editor-grammar Goto Github PK
View Code? Open in Web Editor NEWinvariant codebase of editor-grammar (codemirror-grammar, ace-grammar,prism-grammar,syntaxhighlighter-grammar, highlightjs-grammar,..)
Home Page: https://foo123.github.io/
invariant codebase of editor-grammar (codemirror-grammar, ace-grammar,prism-grammar,syntaxhighlighter-grammar, highlightjs-grammar,..)
Home Page: https://foo123.github.io/
I wanna change syntax rule to allow trailing comma in JSON arrays, so I change
"literal_array" : "'[' (literal_value (',' literal_value)*)? ']'"
to
"literal_array" : "'[' (literal_value (',' literal_value)* (',')? )? ']'"
But seems it not works. What I do wrong?
Error:
error: Token "atom": true|false|null Expected | Token "string" Expected | Token "number" Expected | Token "[" Expected | Token "{" Expected
I see in the document that ^^ defines start of file
I tried to define a language that only contains foo and bar
But the input is "foo bar foo bar", the second "foo bar" is still matched and styled, the ^^ does not seemed to work.
I would like to convert textmate JSON language definitions (like those used in VSCode) to this format (so I can convert to other things.) Is this possible? Has someone already made a tool for this?
This is to remind and propose (for anyone interested) that since EBNF
grammar notations/specifications do not use JSON
format, but a rather similar textual format, for example
EBNF
a_rule ::= symbol1
| symbol2
| 'literal symbol'
| ..
;
or even:
EBNF
a_rule ::= <symbol1>
| <symbol2>
| 'literal symbol'
| ..
;
Although it is rather straightforward to transform the above examples into a JSON
grammar for highlight. It is even better to have a transformation tool for that.
Given that JSON
grammar (as used by *-grammar
highlighter add-ons) can accept and parse EBNF
/PEG
notation. The task is even easier. For example, just split each rule as rule_name => rule_definition
and create a JSON
-like object from that (more correctly the Syntax
part of the overall JSON
grammar).
In the manual, it says
a syntax token can contain (direct or indirect) recursive references to itself ( note: some rule factoring may be needed, to avoid grammar left-recursion or ambiguity )
When I define left-recursive rule in Syntax, I got the exception: Uncaught RangeError: Maximum call stack size exceeded
What do you mean by " some rule factoring may be needed"? Do you mean I need to convert left-recursion to non-left-recursion following rules like this: https://web.cs.wpi.edu/~kal/PLT/PLT4.1.2.html ?
In the following line, I am trying to catch 'var', '=' and 'somevar' separately and assign styles.
var=somevar
I have defined in the lex section 'var', eq and 'subs', and these work well.
,"var" : "RE::/(var)(?:(?=\\s)|(?=\\t)|(?==)|$)/gim"
,"eq" : "RE::/\\s*=\\s*/gim"
,"subs" : "RE::/[A-Za-z$][A-Za-z0-9$]*/"
I am having trouble with the 'somevar' part as I believe I can only identify it accurately as a subs type following after types var and eq.
My sequence is currently
"Syntax" : {
"subs1" : {"sequence":["var", "eq", "subs"]}
,"js" : " comment | eq | operator | var | subs1 | subs | keyword "
},
Am I being too hopeful, or is there a correct approach to the above requirement.
Having the following grammar:
// a partial javascript grammar in simple JSON format
{
// prefix ID for regular expressions used in the grammar
"RegExpID" : "RE::",
// Style model
"Style" : {
"comment" : "comment"
,"atom" : "atom"
,"keyword" : "keyword"
,"this" : "keyword"
,"builtin" : "builtin"
,"operator" : "operator"
,"identifier" : "variable"
,"property" : "attribute"
,"number" : "number"
,"string" : "string"
,"date" : "string-2"
,"regex" : "string-2"
},
// Lexical model
"Lex" : {
"comment" : {"type":"comment","tokens":[
// line comment
// start, end delims (null matches end-of-line)
[ "//", null ],
// block comments
// start, end delims
[ "/*", "*/" ]
]}
,"identifier" : "RE::/[_A-Za-z$][_A-Za-z0-9$]*/"
,"this" : "RE::/this\\b/"
,"property" : "RE::/[_A-Za-z$][_A-Za-z0-9$]*/"
,"date" : "RE::/\\d{4}-\\d{2}-\\d{2}$]*/"
,"number" : [
// floats
"RE::/\\d*\\.\\d+(e[\\+\\-]?\\d+)?/",
"RE::/\\d+\\.\\d*/",
"RE::/\\.\\d+/",
// integers
// hex
"RE::/0x[0-9a-fA-F]+L?/",
// binary
"RE::/0b[01]+L?/",
// octal
"RE::/0o[0-7]+L?/",
// decimal
"RE::/[1-9]\\d*(e[\\+\\-]?\\d+)?L?/",
// just zero
"RE::/0(?![\\dx])/"
]
,"string" : {"type":"escaped-block","escape":"\\","tokens":
// start, end of string (can be the matched regex group ie. 1 )
[ "RE::/(['\"])/", 1 ]
}
,"regex" : {"type":"escaped-block","escape":"\\","tokens":
// javascript literal regular expressions can be parsed similar to strings
[ "/", "RE::#/[gimy]{0,4}#" ]
}
,"operator" : {"tokens":[
"+", "-", "++", "--", "%", ">>", "<<", ">>>",
"*", "/", "^", "|", "&", "!", "~",
">", "<", "<=", ">=", "!=", "!==",
"=", "==", "===", "+=", "-=", "%=",
">>=", ">>>=", "<<=", "*=", "/=", "|=", "&="
]}
,"delimiter" : {"tokens":[
"(", ")", "[", "]", "{", "}", ",", "=", ";", "?", ":",
"+=", "-=", "*=", "/=", "%=", "&=", "|=", "^=", "++", "--",
">>=", "<<="
]}
,"atom" : {"autocomplete":true,"tokens":[
"null", "undefined",
"NaN", "Infinity"
]}
,"boolean" : {"autocomplete":true,"tokens":["true", "false"]}
,"keyword" : {"autocomplete":true,"tokens":[
"if", "while", "with", "else", "do", "try", "finally",
"return", "break", "continue", "new", "delete", "throw",
"var", "const", "let", "function", "catch", "void",
"for", "switch", "case", "default", "class", "import", "yield",
"in", "typeof", "instanceof"
]}
,"builtin" : {"autocomplete":true,"tokens":[
"Object", "Function", "Array", "String",
"Date", "Number", "RegExp", "Math", "Exception",
"setTimeout", "setInterval", "parseInt", "parseFloat",
"isFinite", "isNan", "alert", "prompt", "console",
"window", "global", "this"
]}
},
// Syntax model (optional)
"Syntax" : {
"dot_property" : {"sequence":[".", "property"]},
"prim" : "number | string | boolean",
"kv" : {"sequence": ["string", "':'", "json"]},
"json" : "object | array | prim",
"object" : "'{' kv (',' kv)* '}'",
"array" : "'[' json (',' json)* ']'",
"a" : "'\"and\"'",
"relation" : "'[' op ',' operand ',' operand ']'",
"op" : "'\"=\"' | '\">\"' | '\">=\"' | '\"<\"' | '\"<=\"'",
"operand" : "identifier | date | string | number | boolean",
"criteria" : "relation | and | or",
"and" : "'[' '\"and\"' (',' criteria)+ ']'",
"or" : "'[' '\"or\"' (',' criteria)+ ']'",
"query" : "'{' '\"query\"' ':' string ',' '\"where\"' ':' criteria '}'",
"top" : "query"
},
// what to parse and in what order
"Parser" : [ ["top"] ]
}
I'd expect the input :
{"query": "sdasdas",
"where":
["and",
["=", 1, 1],
["=", 1, 1],
["or",
["=", 1, 1],
["=", 1, 1]
]
]
}
would have no syntax errors, but the there is an error reported.
How do I define recursive grammar like this one?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.