Update rustpeg to latest version and follow new syntax and formatting rules

Signed-off-by: Victor Porof <vporof@mozilla.com>
This commit is contained in:
Victor Porof 2017-02-02 11:36:53 +01:00
parent 611fbe2eef
commit 72da5722ae
2 changed files with 48 additions and 56 deletions

View file

@ -14,4 +14,4 @@ num = "0.1.35"
ordered-float = "0.3.0" ordered-float = "0.3.0"
[build-dependencies] [build-dependencies]
peg = "0.4" peg = "0.5.1"

View file

@ -27,28 +27,25 @@ use types::Value;
// TODO: Support tagged elements // TODO: Support tagged elements
// TODO: Support discard // TODO: Support discard
#[export] pub nil -> Value =
nil -> Value = "nil" { "nil" { Value::Nil }
Value::Nil
}
#[export] pub boolean -> Value =
boolean -> Value =
"true" { Value::Boolean(true) } / "true" { Value::Boolean(true) } /
"false" { Value::Boolean(false) } "false" { Value::Boolean(false) }
digit = [0-9] digit = [0-9]
sign = "-" / "+" sign = "-" / "+"
#[export] pub bigint -> Value =
bigint -> Value = b:$( sign? digit+ ) "N" { b:$( sign? digit+ ) "N" {
Value::BigInteger(b.parse::<BigInt>().unwrap()) Value::BigInteger(b.parse::<BigInt>().unwrap())
} }
#[export] pub integer -> Value =
integer -> Value = i:$( sign? digit+ ) { i:$( sign? digit+ ) {
Value::Integer(i.parse::<i64>().unwrap()) Value::Integer(i.parse::<i64>().unwrap())
} }
frac = sign? digit+ "." digit+ frac = sign? digit+ "." digit+
exp = sign? digit+ ("e" / "E") sign? digit+ exp = sign? digit+ ("e" / "E") sign? digit+
@ -56,10 +53,10 @@ frac_exp = sign? digit+ "." digit+ ("e" / "E") sign? digit+
// The order here is important - frac_exp must come before (exp / frac) or the // The order here is important - frac_exp must come before (exp / frac) or the
// parser assumes exp or frac when the float is really a frac_exp and fails // parser assumes exp or frac when the float is really a frac_exp and fails
#[export] pub float -> Value =
float -> Value = f:$( frac_exp / exp / frac ) { f:$( frac_exp / exp / frac ) {
Value::Float(OrderedFloat(f.parse::<f64>().unwrap())) Value::Float(OrderedFloat(f.parse::<f64>().unwrap()))
} }
// TODO: \newline, \return, \space and \tab // TODO: \newline, \return, \space and \tab
special_char = quote / tab special_char = quote / tab
@ -67,10 +64,10 @@ quote = "\\\""
tab = "\\tab" tab = "\\tab"
char = [^"] / special_char char = [^"] / special_char
#[export] pub text -> Value =
text -> Value = "\"" t:$( char* ) "\"" { "\"" t:$( char* ) "\"" {
Value::Text(t.to_string()) Value::Text(t.to_string())
} }
namespace_divider = "." namespace_divider = "."
namespace_separator = "/" namespace_separator = "/"
@ -91,56 +88,51 @@ keyword_namespace = keyword_namespace_char+ (namespace_divider keyword_namespace
keyword_name_char = [a-z] / [A-Z] / [0-9] / "." keyword_name_char = [a-z] / [A-Z] / [0-9] / "."
keyword_name = keyword_name_char+ keyword_name = keyword_name_char+
#[export] pub symbol -> Value =
symbol -> Value ns:( sns:$(symbol_namespace) namespace_separator { sns })? n:$(symbol_name) {
= ns:( sns:$(symbol_namespace) namespace_separator { sns })? n:$(symbol_name) {
types::to_symbol(ns, n) types::to_symbol(ns, n)
} }
#[export] pub keyword -> Value =
keyword -> Value keyword_prefix ns:( kns:$(keyword_namespace) namespace_separator { kns })? n:$(keyword_name) {
= keyword_prefix ns:( kns:$(keyword_namespace) namespace_separator { kns })? n:$(keyword_name) {
types::to_keyword(ns, n) types::to_keyword(ns, n)
} }
#[export] pub list -> Value =
list -> Value = "(" v:(value)* ")" { "(" v:(value)* ")" {
Value::List(LinkedList::from_iter(v)) Value::List(LinkedList::from_iter(v))
} }
#[export] pub vector -> Value =
vector -> Value = "[" v:(value)* "]" { "[" v:(value)* "]" {
Value::Vector(v) Value::Vector(v)
} }
#[export] pub set -> Value =
set -> Value = "#{" v:(value)* "}" { "#{" v:(value)* "}" {
Value::Set(BTreeSet::from_iter(v)) Value::Set(BTreeSet::from_iter(v))
} }
pair -> (Value, Value) = k:(value) v:(value) { pair -> (Value, Value) =
(k, v) k:(value) v:(value) {
} (k, v)
}
#[export] pub map -> Value =
map -> Value = "{" v:(pair)* "}" { "{" v:(pair)* "}" {
Value::Map(BTreeMap::from_iter(v)) Value::Map(BTreeMap::from_iter(v))
} }
// It's important that float comes before integer or the parser assumes that // It's important that float comes before integer or the parser assumes that
// floats are integers and fails to parse // floats are integers and fails to parse
#[export] pub value -> Value =
value -> Value __ v:(nil / boolean / float / bigint / integer / text / keyword / symbol / list / vector / map / set) __ {
= __ v:(nil / boolean / float / bigint / integer / text / v
keyword / symbol / }
list / vector / map / set) __ {
v
}
// Clojure (and thus EDN) regards commas as whitespace, and thus the two-element vectors [1 2] and // Clojure (and thus EDN) regards commas as whitespace, and thus the two-element vectors [1 2] and
// [1,,,,2] are equivalent, as are the maps {:a 1, :b 2} and {:a 1 :b 2}. // [1,,,,2] are equivalent, as are the maps {:a 1, :b 2} and {:a 1 :b 2}.
whitespace = (" " / "\r" / "\n" / "\t" / ",") whitespace = (" " / "\r" / "\n" / "\t" / ",")
comment = ";" [^\r\n]* ("\r" / "\n")? comment = ";" [^\r\n]* ("\r" / "\n")?
__ = (whitespace / comment)* __ = (whitespace / comment)*