|
1 | 1 | // 1. a partial javascript grammar in simple JSON format
|
2 | 2 | var js_grammar = {
|
3 | 3 |
|
4 |
| -// prefix ID for regular expressions used in the grammar |
5 |
| -"RegExpID" : "RE::", |
6 |
| - |
7 |
| -"Extra" : { |
| 4 | +"RegExpID" : "RE::", |
| 5 | + |
| 6 | +"Extra" : { |
8 | 7 |
|
9 |
| - "fold" : "brace" |
| 8 | + "fold" : "brace" |
10 | 9 |
|
11 | 10 | },
|
12 |
| - |
| 11 | + |
13 | 12 | // Style model
|
14 |
| -"Style" : { |
15 |
| - |
16 |
| - "comment" : "comment" |
17 |
| - ,"atom" : "atom" |
18 |
| - ,"keyword" : "keyword" |
19 |
| - ,"builtin" : "keyword" |
20 |
| - ,"operator" : "operator" |
21 |
| - ,"identifier" : "variable" |
22 |
| - ,"property" : "attribute" |
23 |
| - ,"number" : "number" |
24 |
| - ,"string" : "string" |
25 |
| - ,"regex" : "string-2" |
26 |
| - ,"operator" : "variable-2" |
27 |
| - |
| 13 | +"Style" : { |
| 14 | + |
| 15 | + "comment" : "comment" |
| 16 | + ,"atom" : "atom" |
| 17 | + ,"keyword" : "keyword" |
| 18 | + ,"builtin" : "builtin" |
| 19 | + ,"operator" : "operator" |
| 20 | + ,"identifier" : "variable" |
| 21 | + ,"property" : "attribute" |
| 22 | + ,"number" : "number" |
| 23 | + ,"string" : "string" |
| 24 | + ,"regex" : "string-2" |
| 25 | + |
28 | 26 | },
|
29 | 27 |
|
30 | 28 | // Lexical model
|
31 |
| -"Lex" : { |
32 |
| - |
33 |
| - "comment:comment" : {"interleave":true,"tokens":[ |
34 |
| - // line comment |
35 |
| - [ "//", null ], |
36 |
| - // block comments |
37 |
| - [ "/*", "*/" ] |
38 |
| - ]} |
39 |
| - ,"identifier" : "RE::/[_A-Za-z$][_A-Za-z0-9$]*/" |
40 |
| - ,"number" : [ |
41 |
| - // floats |
42 |
| - "RE::/\\d*\\.\\d+(e[\\+\\-]?\\d+)?/", |
43 |
| - "RE::/\\d+\\.\\d*/", |
44 |
| - "RE::/\\.\\d+/", |
45 |
| - // integers |
46 |
| - // hex |
47 |
| - "RE::/0x[0-9a-fA-F]+L?/", |
48 |
| - // binary |
49 |
| - "RE::/0b[01]+L?/", |
50 |
| - // octal |
51 |
| - "RE::/0o[0-7]+L?/", |
52 |
| - // decimal |
53 |
| - "RE::/[1-9]\\d*(e[\\+\\-]?\\d+)?L?/", |
54 |
| - // just zero |
55 |
| - "RE::/0(?![\\dx])/" |
56 |
| - ] |
57 |
| - ,"string:escaped-block" : ["RE::/(['\"])/", 1] |
58 |
| - ,"regex:escaped-line-block" : ["/", "RE::#/[gimy]{0,4}#"] |
59 |
| - ,"atom" : {"autocomplete":true,"meta":"JavaScript Atom","tokens":[ |
60 |
| - "true", "false", |
61 |
| - "null", "undefined", |
62 |
| - "NaN", "Infinity" |
63 |
| - ]} |
64 |
| - ,"operator" : {"combine":false,"tokens":[ |
65 |
| - "+", "-", "++", "--", "%", ">>", "<<", ">>>", |
66 |
| - "*", "/", "^", "|", "&", "!", "~", |
67 |
| - ">", "<", "<=", ">=", "!=", "!==", |
68 |
| - "=", "==", "===", "+=", "-=", "%=", |
69 |
| - ">>=", ">>>=", "<<=", "*=", "/=", "|=", "&=" |
70 |
| - ]} |
71 |
| - ,"keyword" : {"autocomplete":true,"meta":"JavaScript Keyword","tokens":[ |
72 |
| - "if", "while", "with", "else", "do", "try", "finally", |
73 |
| - "return", "break", "continue", "new", "delete", "throw", |
74 |
| - "var", "const", "let", "function", "catch", "void", |
75 |
| - "for", "switch", "case", "default", "class", "import", "yield", |
76 |
| - "in", "typeof", "instanceof", "?", ":" |
77 |
| - ]} |
78 |
| - ,"builtin" : {"autocomplete":true,"meta":"JavaScript Builtin","tokens":[ |
79 |
| - "Object", "Function", "Array", "String", |
80 |
| - "Date", "Number", "RegExp", "Math", "Exception", |
81 |
| - "setTimeout", "setInterval", "parseInt", "parseFloat", |
82 |
| - "isFinite", "isNan", "alert", "prompt", "console", |
83 |
| - "window", "global", "this" |
84 |
| - ]} |
85 |
| - ,"builtin_property" : {"autocomplete":true,"meta":"JavaScript Builtin Property","tokens":[ |
86 |
| - "prototype","constructor","toString" |
87 |
| - ]} |
88 |
| - ,"ctx:action" : {"context":true} |
89 |
| - ,"\\ctx:action" : {"context":false} |
90 |
| - ,"_match:action" : {"push":"$0"} |
91 |
| - ,"match_bra:action" : {"pop":"{","msg":"Bracket \"$0\" does not match"} |
92 |
| - ,"match_paren:action" : {"pop":"(","msg":"Bracket \"$0\" does not match"} |
93 |
| - ,"match_bpa:action" : {"pop":"[","msg":"Bracket \"$0\" does not match"} |
94 |
| - ,"unique_in_scope:action" : {"unique":["prop","$1"],"in-context":true,"msg":"Duplicate object property \"$0\""} |
| 29 | +"Lex" : { |
95 | 30 |
|
96 |
| -}, |
| 31 | + "comment:comment" : {"interleave":true,"tokens":[ |
| 32 | + // line comment |
| 33 | + [ "//", null ], |
| 34 | + // block comments |
| 35 | + [ "/*", "*/" ] |
| 36 | + ]} |
| 37 | + ,"identifier" : "RE::/[_A-Za-z$][_A-Za-z0-9$]*/" |
| 38 | + ,"number" : [ |
| 39 | + // floats |
| 40 | + "RE::/\\d*\\.\\d+(e[\\+\\-]?\\d+)?/", |
| 41 | + "RE::/\\d+\\.\\d*/", |
| 42 | + "RE::/\\.\\d+/", |
| 43 | + // integers |
| 44 | + // hex |
| 45 | + "RE::/0x[0-9a-fA-F]+L?/", |
| 46 | + // binary |
| 47 | + "RE::/0b[01]+L?/", |
| 48 | + // octal |
| 49 | + "RE::/0o[0-7]+L?/", |
| 50 | + // decimal |
| 51 | + "RE::/[1-9]\\d*(e[\\+\\-]?\\d+)?L?/", |
| 52 | + // just zero |
| 53 | + "RE::/0(?![\\dx])/" |
| 54 | + ] |
| 55 | + ,"string:escaped-block" : [ "RE::/(['\"])/", 1 ] |
| 56 | + ,"regex:escaped-line-block" : [ "/", "RE::#/[gimy]{0,4}#" ] |
| 57 | + ,"atom" : {"autocomplete":true,"meta":"JavaScript Atom","tokens":[ |
| 58 | + "this", |
| 59 | + "true", "false", |
| 60 | + "null", "undefined", |
| 61 | + "NaN", "Infinity" |
| 62 | + ]} |
| 63 | + ,"keyword" : {"autocomplete":true,"meta":"JavaScript Keyword","tokens":[ |
| 64 | + "if", "while", "with", "else", "do", "try", "finally", |
| 65 | + "return", "break", "continue", "new", "delete", "throw", |
| 66 | + "var", "const", "let", "function", "catch", |
| 67 | + "for", "switch", "case", "default", |
| 68 | + "in", "typeof", "instanceof" |
| 69 | + ]} |
| 70 | + ,"builtin" : {"autocomplete":true,"meta":"JavaScript Builtin","tokens":[ |
| 71 | + "Object", "Function", "Array", "String", "Date", "Number", "RegExp", "Exception", |
| 72 | + "setTimeout", "setInterval", "alert", "console", 'window', 'prototype', 'constructor' |
| 73 | + ]} |
| 74 | + ,"other" : "RE::/\\S+/" |
| 75 | + |
| 76 | + ,"ctx:action" : {"context":true} |
| 77 | + ,"\\ctx:action" : {"context":false} |
| 78 | + ,"match_b:action" : {"push":"}"} |
| 79 | + ,"match_p:action" : {"push":")"} |
| 80 | + ,"match_p2:action" : {"push":"]"} |
| 81 | + ,"\\match:action" : {"pop":"$0","msg":"Brackets do not match"} |
| 82 | + ,"unique:action" : {"unique":["prop","$1"],"msg":"Duplicate object property \"$0\"","in-context":true} |
97 | 83 |
|
| 84 | +}, |
| 85 | + |
98 | 86 | // Syntax model (optional)
|
99 |
| -"Syntax" : { |
100 |
| - |
101 |
| - "obj_property" : "string | /0|[1-9][0-9]*/.number | identifier" |
102 |
| - ,"dot_property" : "'.' (builtin_property.builtin | identifier.property)" |
103 |
| - ,"bra_property" : "'[' _match expression ']' match_bpa" |
104 |
| - ,"with_property" : "dot_property | bra_property" |
105 |
| - ,"value" : "builtin | string | regex | identifier | array | object" |
106 |
| - ,"property_value" : "(builtin_property.builtin | obj_property.property) unique_in_scope ':' expression" |
107 |
| - ,"object" : "'{' ctx _match (property_value (',' property_value)*)? '}' match_bra \\ctx" |
108 |
| - ,"array" : "'[' _match (expression (',' expression)*)? ']' match_bpa" |
109 |
| - ,"brackets_matched" : "'{' _match | '}' match_bra | '(' _match | ')' match_paren | '[' _match | ']' match_bpa" |
110 |
| - ,"expression" : "('(' _match)? ((atom | number | value with_property*) (operator expression)?)? (')' match_paren)?" |
111 |
| - ,"js" : "comment | keyword | expression | brackets_matched" |
| 87 | +"Syntax" : { |
112 | 88 |
|
| 89 | + "literalProperty1" : "string | /0|[1-9][0-9]*/ | identifier" |
| 90 | + // back-reference, should be handled |
| 91 | + ,"literalProperty" : "literalProperty1" |
| 92 | + ,"literalValue" : "atom | string | regex | number | identifier | literalArray | literalObject" |
| 93 | + // here, modifier should apply to all of "literalProperty1", via back-reference chain |
| 94 | + ,"literalPropertyValue" : "literalProperty.property unique ':' literalValue" |
| 95 | + // grammar recursion here |
| 96 | + ,"literalObject" : "'{' match_b ctx (literalPropertyValue (',' literalPropertyValue)*)? '}' \\match \\ctx" |
| 97 | + // grammar recursion here |
| 98 | + ,"literalArray" : "'[' match_p2 (literalValue (',' literalValue)*)? ']' \\match" |
| 99 | + ,"brackets" : "'{' match_b | '}' \\match | '(' match_p | ')' \\match | '[' match_p2 | ']' \\match" |
| 100 | + ,"js" : "comment | number | string | regex | keyword | operator | atom | literalObject | literalArray | brackets | other" |
| 101 | + |
113 | 102 | },
|
114 | 103 |
|
115 | 104 | // what to parse and in what order
|
116 |
| -"Parser" : [ ["js"] ] |
| 105 | +"Parser" : [ ["js"] ] |
117 | 106 |
|
118 | 107 | };
|
0 commit comments