This repository was archived by the owner on Sep 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 116
Have the EDN parser store keywords and symbols as rich types. Fixes #154. #163
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,9 +12,11 @@ | |
|
|
||
| use std::collections::{BTreeSet, BTreeMap, LinkedList}; | ||
| use std::iter::FromIterator; | ||
|
|
||
| use num::BigInt; | ||
| use types::Value; | ||
| use ordered_float::OrderedFloat; | ||
| use types; | ||
| use types::Value; | ||
|
|
||
| // Goal: Be able to parse https://github.com/edn-format/edn | ||
| // Also extensible to help parse http://docs.datomic.com/query.html | ||
|
|
@@ -71,23 +73,36 @@ text -> Value = "\"" t:$( char* ) "\"" { | |
| Value::Text(t.to_string()) | ||
| } | ||
|
|
||
| namespace_divider = "." | ||
| namespace_separator = "/" | ||
|
|
||
| // TODO: Be more picky here | ||
| symbol_char_initial = [a-z] / [A-Z] / [0-9] / [*!_?$%&=<>/.] | ||
| symbol_char_subsequent = [a-z] / [A-Z] / [0-9] / [*!_?$%&=<>/.] / "-" | ||
| symbol_char_initial = [a-z] / [A-Z] / [0-9] / [*!_?$%&=<>] | ||
| symbol_char_subsequent = [a-z] / [A-Z] / [0-9] / [-*!_?$%&=<>] | ||
|
|
||
| #[export] | ||
| symbol -> Value = s:$( symbol_char_initial symbol_char_subsequent* ) { | ||
| Value::Symbol(s.to_string()) | ||
| } | ||
| symbol_namespace = symbol_char_initial+ (namespace_divider symbol_char_subsequent+)* | ||
| symbol_name = ( symbol_char_initial+ / "." ) ( symbol_char_subsequent* / "." ) | ||
|
|
||
| keyword_prefix = ":" | ||
|
|
||
| keyword_char_initial = ":" | ||
| // TODO: More chars here? | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this comment still relevant?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I decided not to attack that yet. Start narrow and broaden later. |
||
| keyword_char_subsequent = [a-z] / [A-Z] / [0-9] / "/" | ||
| keyword_namespace_char = [a-z] / [A-Z] / [0-9] | ||
| keyword_namespace = keyword_namespace_char+ (namespace_divider keyword_namespace_char+)* | ||
|
|
||
| keyword_name_char = [a-z] / [A-Z] / [0-9] / "." | ||
| keyword_name = keyword_name_char+ | ||
|
|
||
| #[export] | ||
| keyword -> Value = k:$( keyword_char_initial keyword_char_subsequent+ ) { | ||
| Value::Keyword(k.to_string()) | ||
| } | ||
| symbol -> Value | ||
| = ns:( sns:$(symbol_namespace) namespace_separator { sns })? n:$(symbol_name) { | ||
| types::to_symbol(ns, n) | ||
| } | ||
|
|
||
| #[export] | ||
| keyword -> Value | ||
| = keyword_prefix ns:( kns:$(keyword_namespace) namespace_separator { kns })? n:$(keyword_name) { | ||
| types::to_keyword(ns, n) | ||
| } | ||
|
|
||
| #[export] | ||
| list -> Value = "(" __ v:(__ value)* __ ")" { | ||
|
|
||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| // Copyright 2016 Mozilla | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); you may not use | ||
| // this file except in compliance with the License. You may obtain a copy of the | ||
| // License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| // Unless required by applicable law or agreed to in writing, software distributed | ||
| // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | ||
| // CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations under the License. | ||
|
|
||
| /// A simplification of Clojure's Symbol. | ||
| #[derive(Clone,Debug,Eq,Hash,Ord,PartialOrd,PartialEq)] | ||
| pub struct PlainSymbol(pub String); | ||
|
|
||
| #[derive(Clone,Debug,Eq,Hash,Ord,PartialOrd,PartialEq)] | ||
| pub struct NamespacedSymbol { | ||
| // We derive PartialOrd, which implements a lexicographic based | ||
| // on the order of members, so put namespace first. | ||
| pub namespace: String, | ||
| pub name: String, | ||
| } | ||
|
|
||
| /// A keyword is a symbol, optionally with a namespace, that prints with a leading colon. | ||
| /// This concept is imported from Clojure, as it features in EDN and the query | ||
| /// syntax that we use. | ||
| /// | ||
| /// Clojure's constraints are looser than ours, allowing empty namespaces or | ||
| /// names: | ||
| /// | ||
| /// ```clojure | ||
| /// user=> (keyword "" "") | ||
| /// :/ | ||
| /// user=> (keyword "foo" "") | ||
| /// :foo/ | ||
| /// user=> (keyword "" "bar") | ||
| /// :/bar | ||
| /// ``` | ||
| /// | ||
| /// We think that's nonsense, so we only allow keywords like `:bar` and `:foo/bar`, | ||
| /// with both namespace and main parts containing no whitespace and no colon or slash: | ||
| /// | ||
| /// ```rust | ||
| /// # use edn::symbols::Keyword; | ||
| /// # use edn::symbols::NamespacedKeyword; | ||
| /// let bar = Keyword::new("bar"); // :bar | ||
| /// let foo_bar = NamespacedKeyword::new("foo", "bar"); // :foo/bar | ||
| /// assert_eq!("bar", bar.0); | ||
| /// assert_eq!("bar", foo_bar.name); | ||
| /// assert_eq!("foo", foo_bar.namespace); | ||
| /// ``` | ||
| /// | ||
| /// If you're not sure whether your input is well-formed, you should use a | ||
| /// parser or a reader function first to validate. TODO: implement `read`. | ||
| /// | ||
| /// Callers are expected to follow these rules: | ||
| /// http://www.clojure.org/reference/reader#_symbols | ||
| /// | ||
| /// Future: fast equality (interning?) for keywords. | ||
| /// | ||
| #[derive(Clone,Debug,Eq,Hash,Ord,PartialOrd,PartialEq)] | ||
| pub struct Keyword(pub String); | ||
|
|
||
| #[derive(Clone,Debug,Eq,Hash,Ord,PartialOrd,PartialEq)] | ||
| pub struct NamespacedKeyword { | ||
| // We derive PartialOrd, which implements a lexicographic based | ||
| // on the order of members, so put namespace first. | ||
| pub namespace: String, | ||
| pub name: String, | ||
| } | ||
|
|
||
| impl PlainSymbol { | ||
| pub fn new(name: &str) -> Self { | ||
| assert!(!name.is_empty(), "Symbols cannot be unnamed."); | ||
|
|
||
| return PlainSymbol(name.to_string()); | ||
| } | ||
| } | ||
|
|
||
| impl NamespacedSymbol { | ||
| pub fn new(namespace: &str, name: &str) -> Self { | ||
| assert!(!name.is_empty(), "Symbols cannot be unnamed."); | ||
| assert!(!namespace.is_empty(), "Symbols cannot have an empty non-null namespace."); | ||
|
|
||
| return NamespacedSymbol { name: name.to_string(), namespace: namespace.to_string() }; | ||
| } | ||
| } | ||
|
|
||
| impl Keyword { | ||
| pub fn new(name: &str) -> Self { | ||
| assert!(!name.is_empty(), "Keywords cannot be unnamed."); | ||
|
|
||
| return Keyword(name.to_string()); | ||
| } | ||
| } | ||
|
|
||
| impl NamespacedKeyword { | ||
| pub fn new(namespace: &str, name: &str) -> Self { | ||
| assert!(!name.is_empty(), "Keywords cannot be unnamed."); | ||
| assert!(!namespace.is_empty(), "Keywords cannot have an empty non-null namespace."); | ||
|
|
||
| // TODO: debug asserts to ensure that neither field matches [ :/]. | ||
| return NamespacedKeyword { name: name.to_string(), namespace: namespace.to_string() }; | ||
| } | ||
| } | ||
|
|
||
| // | ||
| // Note that we don't currently do any escaping. | ||
| // | ||
|
|
||
| impl ToString for PlainSymbol { | ||
| /// Print the symbol in EDN format. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ```rust | ||
| /// # use edn::symbols::PlainSymbol; | ||
| /// assert_eq!("baz", PlainSymbol::new("baz").to_string()); | ||
| /// ``` | ||
| fn to_string(&self) -> String { | ||
| return format!("{}", self.0); | ||
| } | ||
| } | ||
|
|
||
| impl ToString for NamespacedSymbol { | ||
| /// Print the symbol in EDN format. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ```rust | ||
| /// # use edn::symbols::NamespacedSymbol; | ||
| /// assert_eq!("bar/baz", NamespacedSymbol::new("bar", "baz").to_string()); | ||
| /// ``` | ||
| fn to_string(&self) -> String { | ||
| return format!("{}/{}", self.namespace, self.name); | ||
| } | ||
| } | ||
|
|
||
| impl ToString for Keyword { | ||
| /// Print the keyword in EDN format. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ```rust | ||
| /// # use edn::symbols::Keyword; | ||
| /// assert_eq!(":baz", Keyword::new("baz").to_string()); | ||
| /// ``` | ||
| fn to_string(&self) -> String { | ||
| return format!(":{}", self.0); | ||
| } | ||
| } | ||
|
|
||
| impl ToString for NamespacedKeyword { | ||
| /// Print the keyword in EDN format. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ```rust | ||
| /// # use edn::symbols::NamespacedKeyword; | ||
| /// assert_eq!(":bar/baz", NamespacedKeyword::new("bar", "baz").to_string()); | ||
| /// ``` | ||
| fn to_string(&self) -> String { | ||
| return format!(":{}/{}", self.namespace, self.name); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something I miss from Java was a rough standard of individual imports, sorted alphabetically, inserted by some refactoring and automatically folded by the editor. Minimal code churn which the developer didn't need to think about.
I suspect that rust tooling will catch up shortly. So nothing to do now, mostly thinking aloud.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I decided to do much what we've been doing in Swift and Java: split std out, sort all alphabetically within that division.