support multi line strings

This commit is contained in:
Fabian Jakobs 2010-04-09 12:32:34 +02:00
commit 24dd04d1ba

View file

@ -107,6 +107,9 @@ var keywords = {
getLineTokens = function(line, startState) getLineTokens = function(line, startState)
{ {
// regexp must not have capturing parentheses
// regexps are ordered -> the first match is used
var rules = { var rules = {
start : start :
[ [
@ -119,17 +122,27 @@ getLineTokens = function(line, startState)
regex: "\\/\\*.*?\\*\\/" regex: "\\/\\*.*?\\*\\/"
}, },
{ {
token: "comment", // multi line comment in several lines token: "comment", // multi line comment start
regex: "\\/\\*.*$", regex: "\\/\\*.*$",
next: "comment" next: "comment"
}, },
{ {
token: "string", // single line token: "string", // single line
regex: '["][^"]*["]' regex: '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]'
},
{
token: "string", // multi line string start
regex: '["].*\\\\$',
next: "qqstring"
}, },
{ {
token: "string", // single line token: "string", // single line
regex: "['][^']*[']" regex: "['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']"
},
{
token: "string", // multi line string start
regex: "['].*\\\\$",
next: "qstring"
}, },
{ {
token: "number", // hex token: "number", // hex
@ -173,6 +186,30 @@ getLineTokens = function(line, startState)
token: "comment", // comment spanning whole line token: "comment", // comment spanning whole line
regex: ".+" regex: ".+"
} }
],
"qqstring":
[
{
token: "string",
regex: '(?:(?:\\\\.)|(?:[^"\\\\]))*?"',
next: "start"
},
{
token: "string",
regex: '.+'
}
],
"qstring":
[
{
token: "string",
regex: "(?:(?:\\\\.)|(?:[^'\\\\]))*?'",
next: "start"
},
{
token: "string",
regex: '.+'
}
] ]
}; };
@ -197,6 +234,8 @@ getLineTokens = function(line, startState)
var match, tokens = []; var match, tokens = [];
var lastIndex = 0;
while (match = re.exec(line)) while (match = re.exec(line))
{ {
var token = { var token = {
@ -204,6 +243,11 @@ getLineTokens = function(line, startState)
value: match[0] value: match[0]
} }
if (re.lastIndex == lastIndex) {
throw new Error("tokenizer error")
}
lastIndex = re.lastIndex;
//console.log(match); //console.log(match);
for (var i=0; i < state.length; i++) for (var i=0; i < state.length; i++)