mirror of
https://codeberg.org/icewind/php-literal-parser.git
synced 2026-06-03 18:44:07 +02:00
allow trailing semicolon
This commit is contained in:
parent
20dad94035
commit
688f4fb6f4
4 changed files with 26 additions and 17 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
[package]
|
[package]
|
||||||
name = "php-literal-parser"
|
name = "php-literal-parser"
|
||||||
description = "parser for php literals"
|
description = "parser for php literals"
|
||||||
version = "0.2.2"
|
version = "0.2.3"
|
||||||
authors = ["Robin Appelman <robin@icewind.nl>"]
|
authors = ["Robin Appelman <robin@icewind.nl>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
license = "MIT OR Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,8 @@ pub enum Token {
|
||||||
Float,
|
Float,
|
||||||
#[regex("-?(0|[1-9][0-9]*(_[0-9]+)*|0[xX][0-9a-fA-F]+(_[0-9a-fA-F]+)*|0[0-7]+(_[0-7]+)*|0[bB][01]+(_[01]+)*)")]
|
#[regex("-?(0|[1-9][0-9]*(_[0-9]+)*|0[xX][0-9a-fA-F]+(_[0-9a-fA-F]+)*|0[0-7]+(_[0-7]+)*|0[bB][01]+(_[01]+)*)")]
|
||||||
Integer,
|
Integer,
|
||||||
|
#[token(";")]
|
||||||
|
SemiColon,
|
||||||
#[error]
|
#[error]
|
||||||
#[regex(r"[ \t\n\f]+", logos::skip)]
|
#[regex(r"[ \t\n\f]+", logos::skip)]
|
||||||
Error,
|
Error,
|
||||||
|
|
|
||||||
|
|
@ -52,10 +52,13 @@ where
|
||||||
{
|
{
|
||||||
let mut deserializer = Deserializer::from_str(s);
|
let mut deserializer = Deserializer::from_str(s);
|
||||||
let t = T::deserialize(&mut deserializer)?;
|
let t = T::deserialize(&mut deserializer)?;
|
||||||
if deserializer.next_token().is_none() {
|
match deserializer.next_token() {
|
||||||
Ok(t)
|
None
|
||||||
} else {
|
| Some(SpannedToken {
|
||||||
Err(RawParseError::TrailingCharacters.into())
|
token: Token::SemiColon,
|
||||||
|
..
|
||||||
|
}) => Ok(t),
|
||||||
|
Some(_) => Err(RawParseError::TrailingCharacters.into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,19 @@
|
||||||
|
use maplit::hashmap;
|
||||||
use php_literal_parser::{from_str, Key, ParseError, Value};
|
use php_literal_parser::{from_str, Key, ParseError, Value};
|
||||||
|
|
||||||
|
fn parse(source: &str) -> Result<Value, ParseError> {
|
||||||
|
match from_str(source) {
|
||||||
|
Ok(res) => Ok(res),
|
||||||
|
Err(err) => {
|
||||||
|
let sourced = err.with_source(source);
|
||||||
|
eprintln!("{}", sourced);
|
||||||
|
Err(sourced.into_inner())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_value() {
|
fn test_parse_value() {
|
||||||
fn parse(source: &str) -> Result<Value, ParseError> {
|
|
||||||
match from_str(source) {
|
|
||||||
Ok(res) => Ok(res),
|
|
||||||
Err(err) => {
|
|
||||||
let sourced = err.with_source(source);
|
|
||||||
eprintln!("{}", sourced);
|
|
||||||
Err(sourced.into_inner())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
use maplit::hashmap;
|
|
||||||
|
|
||||||
assert_eq!(Value::Bool(true), parse("true").unwrap());
|
assert_eq!(Value::Bool(true), parse("true").unwrap());
|
||||||
assert_eq!(Value::Bool(false), parse("false").unwrap());
|
assert_eq!(Value::Bool(false), parse("false").unwrap());
|
||||||
assert_eq!(Value::Int(12), parse("12").unwrap());
|
assert_eq!(Value::Int(12), parse("12").unwrap());
|
||||||
|
|
@ -132,3 +131,8 @@ fn test_parse_value() {
|
||||||
parse(r#"array("2"=>3,"foo" => 4, null => 5, true => 6, false => 7)"#).unwrap()
|
parse(r#"array("2"=>3,"foo" => 4, null => 5, true => 6, false => 7)"#).unwrap()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_trailing_semi() {
|
||||||
|
assert_eq!(Value::Int(12), parse(r#"12;"#).unwrap());
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue