使用Parsimmon在JavaScript中解析用户输入的数组
我正在研究如何用JavaScript解析用户输入的数组,例如,使用Parsimmon在JavaScript中解析用户输入的数组,javascript,string-parsing,parser-combinators,Javascript,String Parsing,Parser Combinators,我正在研究如何用JavaScript解析用户输入的数组,例如,“[1,2,3,4,[1,2,3],“test”,“test hello”]”。 现在,我找到了帕西蒙,这似乎是一个很好的工具。该库附带了一个JSON解析器示例,实现方式如下: let JSONParser = P.createLanguage({ // This is the main entry point of the parser: a full JSON value. valu
“[1,2,3,4,[1,2,3],“test”,“test hello”]”
。
现在,我找到了帕西蒙,这似乎是一个很好的工具。该库附带了一个JSON解析器示例,实现方式如下:
let JSONParser = P.createLanguage({
// This is the main entry point of the parser: a full JSON value.
value: r =>
P.alt(
r.object,
r.array,
r.string,
r.number,
r.null,
r.true,
r.false
).thru(parser => whitespace.then(parser)),
// The basic tokens in JSON, with optional whitespace afterward.
lbrace: () => word("{"),
rbrace: () => word("}"),
lbracket: () => word("["),
rbracket: () => word("]"),
comma: () => word(","),
colon: () => word(":"),
// `.result` is like `.map` but it takes a value instead of a function, and
// `.always returns the same value.
null: () => word("null").result(null),
true: () => word("true").result(true),
false: () => word("false").result(false),
// Regexp based parsers should generally be named for better error reporting.
string: () =>
token(P.regexp(/"((?:\\.|.)*?)"/, 1))
.map(interpretEscapes)
.desc("string"),
number: () =>
token(P.regexp(/-?(0|[1-9][0-9]*)([.][0-9]+)?([eE][+-]?[0-9]+)?/))
.map(Number)
.desc("number"),
// Array parsing is just ignoring brackets and commas and parsing as many nested
// JSON documents as possible. Notice that we're using the parser `json` we just
// defined above. Arrays and objects in the JSON grammar are recursive because
// they can contain any other JSON document within them.
array: r => r.lbracket.then(r.value.sepBy(r.comma)).skip(r.rbracket),
// Object parsing is a little trickier because we have to collect all the key-
// value pairs in order as length-2 arrays, then manually copy them into an
// object.
pair: r => P.seq(r.string.skip(r.colon), r.value),
object: r =>
r.lbrace
.then(r.pair.sepBy(r.comma))
.skip(r.rbrace)
.map(pairs => {
let object = {};
pairs.forEach(pair => {
let [key, value] = pair;
object[key] = value;
});
return object;
})
});
现在,我尝试在不使用createLanguage函数的情况下实现我自己的数组解析器,这是我目前的实现:
let comma = word2(',');
let lBracket = word2('[');
let rBracket = word2(']');
let arrayParser = P.lazy(
() =>
// P.seq(
lBracket.then(
P.alt(
P.digits,
// stringParser.trim(P.optWhitespace).sepBy(P.string(','))
// P.digits.sepBy1(P.string(',')),
arrayParser
).thru(parser => P.optWhitespace.then(parser)).sepBy(comma)
).skip(
rBracket.trim(P.optWhitespace)
)
// )
);
该实现用于解析以下内容:
'[1, 2, 3, 4]'
但不用于分析嵌套数组:
'[1, 2, 3, [1, 2, 3], 4]'
如果有人愿意回答,我想知道我做错了什么
谢谢大家! 为什么不
JSON.parse
?您的示例是有效的JSON。如果有一些东西是你不想允许的有效JSON,那么事后很容易发现它们。。。此外,如果用户输入错误,JSON.parse将抛出异常。相反,Parsimmon返回的对象的结果字段为true或false……第二个问题不是问题:使用try
/catch
。但听起来您正在努力创建一个基本的解析器,这表明您最好使用已经创建的解析器。(你也可以调整JSON解析器,但是,你已经有了一个正在调整的解析器,所以很多都不是那么有用……)无论如何,祝你好运!