Skip to content

Commit f7053c5

Browse files
committed
disallow nested unary
1 parent e022239 commit f7053c5

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

src/parser.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -494,28 +494,24 @@ impl<'toks, 'input> JSON5Parser<'toks, 'input> {
494494
match self.check_and_consume(vec![TokType::Plus, TokType::Minus]) {
495495
None => self.parse_primary(),
496496
Some(span) => {
497+
let value = self.parse_unary()?;
498+
match value {
499+
JSONValue::Float(_) | JSONValue::Integer(_) | JSONValue::Infinity | JSONValue::NaN | JSONValue::Hexadecimal(_) | JSONValue::Exponent(_) => {}
500+
JSONValue::Unary{ .. } => {
501+
return Err(self.make_error("Only one unary operator is allowed".to_string(), span.2))
502+
}
503+
val => {
504+
return Err(self.make_error(format!("Unary operations not allowed for value {:?}", val), span.2))
505+
}
506+
}
497507
match span.1 {
498508
TokType::Plus => {
499-
let value = self.parse_unary()?;
500-
match value {
501-
JSONValue::Float(_) | JSONValue::Integer(_) | JSONValue::Infinity | JSONValue::NaN | JSONValue::Unary { .. } | JSONValue::Hexadecimal(_) | JSONValue::Exponent(_) => {}
502-
val => {
503-
return Err(self.make_error(format!("Unary operations not allowed for value {:?}", val), span.2))
504-
}
505-
}
506509
Ok(JSONValue::Unary {operator: UnaryOperator::Plus, value: Box::new(value)})
507510
}
508511
TokType::Minus => {
509-
let value = self.parse_unary()?;
510-
match value {
511-
JSONValue::Float(_) | JSONValue::Integer(_) | JSONValue::Infinity | JSONValue::NaN | JSONValue::Unary { .. } | JSONValue::Hexadecimal(_) | JSONValue::Exponent(_) => {}
512-
val => {
513-
return Err(self.make_error(format!("Unary operations not allowed for value {:?}", val), span.2))
514-
}
515-
}
516512
Ok(JSONValue::Unary {operator: UnaryOperator::Minus, value: Box::new(value)})
517513
}
518-
_ => unreachable!("no")
514+
_ => unreachable!("unexpected unary token type")
519515
}
520516
}
521517
}

src/rt/parser.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,10 @@ impl<'toks, 'input> JSON5Parser<'toks, 'input> {
613613
TokType::Plus => {
614614
let value = self.parse_unary()?;
615615
match value {
616-
JSONValue::Float(_) | JSONValue::Integer(_) | JSONValue::Infinity | JSONValue::NaN | JSONValue::Unary { .. } | JSONValue::Hexadecimal(_) | JSONValue::Exponent(_) => {}
616+
JSONValue::Float(_) | JSONValue::Integer(_) | JSONValue::Infinity | JSONValue::NaN | JSONValue::Hexadecimal(_) | JSONValue::Exponent(_) => {}
617+
JSONValue::Unary{ .. } => {
618+
return Err(self.make_error("Only one unary operator is allowed".to_string(), span.2))
619+
}
617620
val => {
618621
return Err(self.make_error(format!("Unary operations not allowed for value {:?}", val), span.2))
619622
}
@@ -623,7 +626,10 @@ impl<'toks, 'input> JSON5Parser<'toks, 'input> {
623626
TokType::Minus => {
624627
let value = self.parse_unary()?;
625628
match value {
626-
JSONValue::Float(_) | JSONValue::Integer(_) | JSONValue::Infinity | JSONValue::NaN | JSONValue::Unary { .. } | JSONValue::Hexadecimal(_) | JSONValue::Exponent(_) => {}
629+
JSONValue::Float(_) | JSONValue::Integer(_) | JSONValue::Infinity | JSONValue::NaN | JSONValue::Hexadecimal(_) | JSONValue::Exponent(_) => {}
630+
JSONValue::Unary{ .. } => {
631+
return Err(self.make_error("Only one unary operator is allowed".to_string(), span.2))
632+
}
627633
val => {
628634
return Err(self.make_error(format!("Unary operations not allowed for value {:?}", val), span.2))
629635
}

0 commit comments

Comments
 (0)