|
| 1 | +/* vim: set filetype=rust.rustpeg */ |
| 2 | + |
| 3 | +// Copyright 2016 Mozilla |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); you may not use |
| 6 | +// this file except in compliance with the License. You may obtain a copy of the |
| 7 | +// License at http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// Unless required by applicable law or agreed to in writing, software distributed |
| 9 | +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 10 | +// CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | +// specific language governing permissions and limitations under the License. |
| 12 | + |
| 13 | +use std::collections::{BTreeSet, BTreeMap, LinkedList}; |
| 14 | +use std::iter::FromIterator; |
| 15 | +use num::BigInt; |
| 16 | +use types::Value; |
| 17 | +use ordered_float::OrderedFloat; |
| 18 | + |
| 19 | +// Goal: Be able to parse https://github.com/edn-format/edn |
| 20 | +// Also extensible to help parse http://docs.datomic.com/query.html |
| 21 | + |
| 22 | +// Debugging hint: test using `cargo test --features peg/trace -- --nocapture` |
| 23 | +// to trace where the parser is failing |
| 24 | + |
| 25 | +// TODO: Support tagged elements |
| 26 | +// TODO: Support comments |
| 27 | +// TODO: Support discard |
| 28 | + |
| 29 | +#[export] |
| 30 | +nil -> Value = "nil" { |
| 31 | + Value::Nil |
| 32 | +} |
| 33 | + |
| 34 | +#[export] |
| 35 | +boolean -> Value = |
| 36 | + "true" { Value::Boolean(true) } / |
| 37 | + "false" { Value::Boolean(false) } |
| 38 | + |
| 39 | +digit = [0-9] |
| 40 | +sign = "-" / "+" |
| 41 | + |
| 42 | +#[export] |
| 43 | +bigint -> Value = b:$( sign? digit+ ) "N" { |
| 44 | + Value::BigInteger(b.parse::<BigInt>().unwrap()) |
| 45 | +} |
| 46 | + |
| 47 | +#[export] |
| 48 | +integer -> Value = i:$( sign? digit+ ) { |
| 49 | + Value::Integer(i.parse::<i64>().unwrap()) |
| 50 | +} |
| 51 | + |
| 52 | +frac = sign? digit+ "." digit+ |
| 53 | +exp = sign? digit+ ("e" / "E") sign? digit+ |
| 54 | +frac_exp = sign? digit+ "." digit+ ("e" / "E") sign? digit+ |
| 55 | + |
| 56 | +// The order here is important - frac_exp must come before (exp / frac) or the |
| 57 | +// parser assumes exp or frac when the float is really a frac_exp and fails |
| 58 | +#[export] |
| 59 | +float -> Value = f:$( frac_exp / exp / frac ) { |
| 60 | + Value::Float(OrderedFloat(f.parse::<f64>().unwrap())) |
| 61 | +} |
| 62 | + |
| 63 | +// TODO: \newline, \return, \space and \tab |
| 64 | +special_char = quote / tab |
| 65 | +quote = "\\\"" |
| 66 | +tab = "\\tab" |
| 67 | +char = [^"] / special_char |
| 68 | + |
| 69 | +#[export] |
| 70 | +text -> Value = "\"" t:$( char* ) "\"" { |
| 71 | + Value::Text(t.to_string()) |
| 72 | +} |
| 73 | + |
| 74 | +// TODO: Be more picky here |
| 75 | +symbol_char_initial = [a-z] / [A-Z] / [0-9] / [*!_?$%&=<>/.] |
| 76 | +symbol_char_subsequent = [a-z] / [A-Z] / [0-9] / [*!_?$%&=<>/.] / "-" |
| 77 | + |
| 78 | +#[export] |
| 79 | +symbol -> Value = s:$( symbol_char_initial symbol_char_subsequent* ) { |
| 80 | + Value::Symbol(s.to_string()) |
| 81 | +} |
| 82 | + |
| 83 | +keyword_char_initial = ":" |
| 84 | +// TODO: More chars here? |
| 85 | +keyword_char_subsequent = [a-z] / [A-Z] / [0-9] / "/" |
| 86 | + |
| 87 | +#[export] |
| 88 | +keyword -> Value = k:$( keyword_char_initial keyword_char_subsequent+ ) { |
| 89 | + Value::Keyword(k.to_string()) |
| 90 | +} |
| 91 | + |
| 92 | +#[export] |
| 93 | +list -> Value = "(" __ v:(__ value)* __ ")" { |
| 94 | + Value::List(LinkedList::from_iter(v)) |
| 95 | +} |
| 96 | + |
| 97 | +#[export] |
| 98 | +vector -> Value = "[" __ v:(__ value)* __ "]" { |
| 99 | + Value::Vector(v) |
| 100 | +} |
| 101 | + |
| 102 | +#[export] |
| 103 | +set -> Value = "#{" __ v:(__ value)* __ "}" { |
| 104 | + Value::Set(BTreeSet::from_iter(v)) |
| 105 | +} |
| 106 | + |
| 107 | +pair -> (Value, Value) = k:(value) " " v:(value) ", "? { |
| 108 | + (k, v) |
| 109 | +} |
| 110 | + |
| 111 | +#[export] |
| 112 | +map -> Value = "{" __ v:(pair)* __ "}" { |
| 113 | + Value::Map(BTreeMap::from_iter(v)) |
| 114 | +} |
| 115 | + |
| 116 | +// It's important that float comes before integer or the parser assumes that |
| 117 | +// floats are integers and fails to parse |
| 118 | +#[export] |
| 119 | +value -> Value |
| 120 | + = nil / boolean / float / bigint / integer / text / |
| 121 | + keyword / symbol / |
| 122 | + list / vector / map / set |
| 123 | + |
| 124 | +whitespace = (" " / "\r" / "\n" / "\t") |
| 125 | + |
| 126 | +__ = whitespace* |
0 commit comments