fix: parse negative numbers and empty bytestrings

This commit is contained in:
Kasey White 2022-06-10 17:23:36 -04:00
parent 2afded2d27
commit 1ef116fcda
1 changed files with 4 additions and 4 deletions

View File

@ -18,7 +18,7 @@ peg::parser! {
rule version() -> (usize, usize, usize) rule version() -> (usize, usize, usize)
= major:number() "." minor:number() "." patch:number() { = major:number() "." minor:number() "." patch:number() {
(major, minor, patch) (major as usize, minor as usize, patch as usize)
} }
rule term() -> Term<Name> rule term() -> Term<Name>
@ -77,7 +77,7 @@ peg::parser! {
= "integer" _+ i:number() { Constant::Integer(i as isize) } = "integer" _+ i:number() { Constant::Integer(i as isize) }
rule constant_bytestring() -> Constant rule constant_bytestring() -> Constant
= "bytestring" _+ "#" i:ident() { Constant::ByteString(hex::decode(i).unwrap()) } = "bytestring" _+ "#" i:ident()* { Constant::ByteString(hex::decode(String::from_iter(i)).unwrap()) }
rule constant_string() -> Constant rule constant_string() -> Constant
= "string" _+ "\"" s:[^ '"']* "\"" { Constant::String(String::from_iter(s)) } = "string" _+ "\"" s:[^ '"']* "\"" { Constant::String(String::from_iter(s)) }
@ -88,8 +88,8 @@ peg::parser! {
rule constant_unit() -> Constant rule constant_unit() -> Constant
= "unit" _+ "()" { Constant::Unit } = "unit" _+ "()" { Constant::Unit }
rule number() -> usize rule number() -> isize
= n:$(['0'..='9']+) {? n.parse().or(Err("usize")) } = n:$("-"* ['0'..='9']+) {? n.parse().or(Err("isize")) }
rule name() -> Name rule name() -> Name
= text:ident() { Name { text, unique: 0.into() } } = text:ident() { Name { text, unique: 0.into() } }