| 1 |
import {javascript} from "./javascript.js" |
| 2 |
|
| 3 |
function identifier(stream) { |
| 4 |
return stream.match(/^[a-zA-Z_][a-zA-Z0-9_]*/) |
| 5 |
} |
| 6 |
|
| 7 |
export const pegjs = { |
| 8 |
name: "pegjs", |
| 9 |
startState: function () { |
| 10 |
return { |
| 11 |
inString: false, |
| 12 |
stringType: null, |
| 13 |
inComment: false, |
| 14 |
inCharacterClass: false, |
| 15 |
braced: 0, |
| 16 |
lhs: true, |
| 17 |
localState: null |
| 18 |
} |
| 19 |
}, |
| 20 |
token: function (stream, state) { |
| 21 |
if (!state.inString && |
| 22 |
!state.inComment && |
| 23 |
(stream.peek() === '"' || stream.peek() === "'")) { |
| 24 |
//check for state changes |
| 25 |
state.stringType = stream.peek() |
| 26 |
stream.next() // Skip quote |
| 27 |
state.inString = true // Update state |
| 28 |
} |
| 29 |
if (!state.inString && !state.inComment && stream.match("/*")) |
| 30 |
state.inComment = true |
| 31 |
|
| 32 |
if (state.inString) { |
| 33 |
while (state.inString && !stream.eol()) { |
| 34 |
if (stream.peek() === state.stringType) { |
| 35 |
stream.next() // Skip quote |
| 36 |
state.inString = false // Clear flag |
| 37 |
} else if (stream.peek() === "\\") { |
| 38 |
stream.next() |
| 39 |
stream.next() |
| 40 |
} else { |
| 41 |
stream.match(/^.[^\\"']*/) |
| 42 |
} |
| 43 |
} |
| 44 |
return state.lhs ? "property string" : "string" // Token style |
| 45 |
} else if (state.inComment) { |
| 46 |
while (state.inComment && !stream.eol()) { |
| 47 |
if (stream.match("*/")) state.inComment = false // Clear flag |
| 48 |
else stream.match(/^.[^*]*/) |
| 49 |
} |
| 50 |
return "comment" |
| 51 |
} else if (state.inCharacterClass) { |
| 52 |
while (state.inCharacterClass && !stream.eol()) { |
| 53 |
if (!(stream.match(/^[^\]\\]+/) || stream.match(/^\\./))) |
| 54 |
state.inCharacterClass = false |
| 55 |
} |
| 56 |
} else if (stream.peek() === "[") { |
| 57 |
stream.next() |
| 58 |
state.inCharacterClass = true |
| 59 |
return "bracket" |
| 60 |
} else if (stream.match("//")) { |
| 61 |
stream.skipToEnd() |
| 62 |
return "comment" |
| 63 |
} else if (state.braced || stream.peek() === "{") { |
| 64 |
if (state.localState === null) state.localState = javascript.startState() |
| 65 |
var token = javascript.token(stream, state.localState) |
| 66 |
var text = stream.current() |
| 67 |
if (!token) { |
| 68 |
for (var i = 0; i < text.length; i++) { |
| 69 |
if (text[i] === "{") state.braced++ |
| 70 |
else if (text[i] === "}") state.braced-- |
| 71 |
} |
| 72 |
} |
| 73 |
return token |
| 74 |
} else if (identifier(stream)) { |
| 75 |
if (stream.peek() === ":") return "variable" |
| 76 |
return "variable-2" |
| 77 |
} else if (["[", "]", "(", ")"].indexOf(stream.peek() || "") !== -1) { |
| 78 |
stream.next() |
| 79 |
return "bracket" |
| 80 |
} else if (!stream.eatSpace()) { |
| 81 |
stream.next() |
| 82 |
} |
| 83 |
return null |
| 84 |
} |
| 85 |
} |