allow trailing semicolon

This commit is contained in:
Robin Appelman 2021-02-15 15:38:39 +01:00
commit 688f4fb6f4
4 changed files with 26 additions and 17 deletions

View file

@ -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"

View file

@ -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,

View file

@ -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()),
} }
} }

View file

@ -1,7 +1,6 @@
use maplit::hashmap;
use php_literal_parser::{from_str, Key, ParseError, Value}; use php_literal_parser::{from_str, Key, ParseError, Value};
#[test]
fn test_parse_value() {
fn parse(source: &str) -> Result<Value, ParseError> { fn parse(source: &str) -> Result<Value, ParseError> {
match from_str(source) { match from_str(source) {
Ok(res) => Ok(res), Ok(res) => Ok(res),
@ -13,8 +12,8 @@ fn test_parse_value() {
} }
} }
use maplit::hashmap; #[test]
fn test_parse_value() {
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());
}