| 1 |
var commentType = {slash: 0, parenthesis: 1}; |
| 2 |
var stateType = {comment: 0, _string: 1, characterClass: 2}; |
| 3 |
|
| 4 |
export const ebnf = { |
| 5 |
name: "ebnf", |
| 6 |
startState: function () { |
| 7 |
return { |
| 8 |
stringType: null, |
| 9 |
commentType: null, |
| 10 |
braced: 0, |
| 11 |
lhs: true, |
| 12 |
localState: null, |
| 13 |
stack: [], |
| 14 |
inDefinition: false |
| 15 |
}; |
| 16 |
}, |
| 17 |
token: function (stream, state) { |
| 18 |
if (!stream) return; |
| 19 |
|
| 20 |
//check for state changes |
| 21 |
if (state.stack.length === 0) { |
| 22 |
//strings |
| 23 |
if ((stream.peek() == '"') || (stream.peek() == "'")) { |
| 24 |
state.stringType = stream.peek(); |
| 25 |
stream.next(); // Skip quote |
| 26 |
state.stack.unshift(stateType._string); |
| 27 |
} else if (stream.match('/*')) { //comments starting with /* |
| 28 |
state.stack.unshift(stateType.comment); |
| 29 |
state.commentType = commentType.slash; |
| 30 |
} else if (stream.match('(*')) { //comments starting with (* |
| 31 |
state.stack.unshift(stateType.comment); |
| 32 |
state.commentType = commentType.parenthesis; |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
//return state |
| 37 |
//stack has |
| 38 |
switch (state.stack[0]) { |
| 39 |
case stateType._string: |
| 40 |
while (state.stack[0] === stateType._string && !stream.eol()) { |
| 41 |
if (stream.peek() === state.stringType) { |
| 42 |
stream.next(); // Skip quote |
| 43 |
state.stack.shift(); // Clear flag |
| 44 |
} else if (stream.peek() === "\\") { |
| 45 |
stream.next(); |
| 46 |
stream.next(); |
| 47 |
} else { |
| 48 |
stream.match(/^.[^\\\"\']*/); |
| 49 |
} |
| 50 |
} |
| 51 |
return state.lhs ? "property" : "string"; // Token style |
| 52 |
|
| 53 |
case stateType.comment: |
| 54 |
while (state.stack[0] === stateType.comment && !stream.eol()) { |
| 55 |
if (state.commentType === commentType.slash && stream.match('*/')) { |
| 56 |
state.stack.shift(); // Clear flag |
| 57 |
state.commentType = null; |
| 58 |
} else if (state.commentType === commentType.parenthesis && stream.match('*)')) { |
| 59 |
state.stack.shift(); // Clear flag |
| 60 |
state.commentType = null; |
| 61 |
} else { |
| 62 |
stream.match(/^.[^\*]*/); |
| 63 |
} |
| 64 |
} |
| 65 |
return "comment"; |
| 66 |
|
| 67 |
case stateType.characterClass: |
| 68 |
while (state.stack[0] === stateType.characterClass && !stream.eol()) { |
| 69 |
if (!(stream.match(/^[^\]\\]+/) || stream.match('.'))) { |
| 70 |
state.stack.shift(); |
| 71 |
} |
| 72 |
} |
| 73 |
return "operator"; |
| 74 |
} |
| 75 |
|
| 76 |
var peek = stream.peek(); |
| 77 |
|
| 78 |
//no stack |
| 79 |
switch (peek) { |
| 80 |
case "[": |
| 81 |
stream.next(); |
| 82 |
state.stack.unshift(stateType.characterClass); |
| 83 |
return "bracket"; |
| 84 |
case ":": |
| 85 |
case "|": |
| 86 |
case ";": |
| 87 |
stream.next(); |
| 88 |
return "operator"; |
| 89 |
case "%": |
| 90 |
if (stream.match("%%")) { |
| 91 |
return "header"; |
| 92 |
} else if (stream.match(/[%][A-Za-z]+/)) { |
| 93 |
return "keyword"; |
| 94 |
} else if (stream.match(/[%][}]/)) { |
| 95 |
return "bracket"; |
| 96 |
} |
| 97 |
break; |
| 98 |
case "/": |
| 99 |
if (stream.match(/[\/][A-Za-z]+/)) { |
| 100 |
return "keyword"; |
| 101 |
} |
| 102 |
case "\\": |
| 103 |
if (stream.match(/[\][a-z]+/)) { |
| 104 |
return "string.special"; |
| 105 |
} |
| 106 |
case ".": |
| 107 |
if (stream.match(".")) { |
| 108 |
return "atom"; |
| 109 |
} |
| 110 |
case "*": |
| 111 |
case "-": |
| 112 |
case "+": |
| 113 |
case "^": |
| 114 |
if (stream.match(peek)) { |
| 115 |
return "atom"; |
| 116 |
} |
| 117 |
case "$": |
| 118 |
if (stream.match("$$")) { |
| 119 |
return "builtin"; |
| 120 |
} else if (stream.match(/[$][0-9]+/)) { |
| 121 |
return "variableName.special"; |
| 122 |
} |
| 123 |
case "<": |
| 124 |
if (stream.match(/<<[a-zA-Z_]+>>/)) { |
| 125 |
return "builtin"; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
if (stream.match('//')) { |
| 130 |
stream.skipToEnd(); |
| 131 |
return "comment"; |
| 132 |
} else if (stream.match('return')) { |
| 133 |
return "operator"; |
| 134 |
} else if (stream.match(/^[a-zA-Z_][a-zA-Z0-9_]*/)) { |
| 135 |
if (stream.match(/(?=[\(.])/)) { |
| 136 |
return "variable"; |
| 137 |
} else if (stream.match(/(?=[\s\n]*[:=])/)) { |
| 138 |
return "def"; |
| 139 |
} |
| 140 |
return "variableName.special"; |
| 141 |
} else if (["[", "]", "(", ")"].indexOf(stream.peek()) != -1) { |
| 142 |
stream.next(); |
| 143 |
return "bracket"; |
| 144 |
} else if (!stream.eatSpace()) { |
| 145 |
stream.next(); |
| 146 |
} |
| 147 |
return null; |
| 148 |
} |
| 149 |
}; |