Skip to content

Commit

Permalink
Parse LanguageSpecs and Json docs
Browse files Browse the repository at this point in the history
  • Loading branch information
justinpombrio committed Apr 5, 2024
1 parent c293d27 commit 0a19726
Show file tree
Hide file tree
Showing 17 changed files with 561 additions and 46 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ typed-arena = "2.0"
generational-arena = "0.2"
crossterm = "0.27.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
ron = "0.8.1"
[dependencies.partial-pretty-printer]
git = "https://github.com/justinpombrio/partial-pretty-printer"
version = "0.5.1"
version = "0.6.0"
features = ["serialization"]
[dependencies.no-nonsense-flamegraphs]
version = "0.2.*"
Expand Down
172 changes: 172 additions & 0 deletions data/json_lang.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
// TODO: styles

LanguageSpec(
name: "Json",
grammar: GrammarSpec(
constructs: [
ConstructSpec(
name: "Root",
arity: Fixed([SortSpec(["value"])]),
key: None,
),
ConstructSpec(
name: "Null",
arity: Fixed([]),
key: Some('x'),
),
ConstructSpec(
name: "True",
arity: Fixed([]),
key: Some('t'),
),
ConstructSpec(
name: "False",
arity: Fixed([]),
key: Some('f'),
),
ConstructSpec(
name: "String",
arity: Texty,
key: Some('s'),
),
ConstructSpec(
name: "Number",
arity: Texty,
key: Some('n'),
),
ConstructSpec(
name: "Array",
arity: Listy(SortSpec(["value"])),
key: Some('a'),
),
ConstructSpec(
name: "ObjectPair",
arity: Fixed([SortSpec(["String"]), SortSpec(["value"])]),
key: None,
),
ConstructSpec(
name: "Object",
arity: Listy(SortSpec(["ObjectPair"])),
key: Some('o'),
),
],
sorts: [
("value", SortSpec(["Null", "bool", "String", "Number", "Array", "Object"])),
("bool", SortSpec(["True", "False"])),
],
root_construct: "Root",
),
default_display_notation: NotationSetSpec(
name: "DefaultDisplay",
notations: [
("Root", Child(0)),
("Null", Literal("null")),
("True", Literal("true")),
("False", Literal("false")),
("String", Concat(Literal("\""), Concat(Text, Literal("\"")))),
("Number", Check(IsEmptyText, Here, Literal("•"), Text)),
("Array",
Count(
zero:
Concat(Style(Open, Literal("[")),
Concat(FocusMark,
Style(Close, Literal("]")))),
one: Choice(
// single line
Concat(Style(Open, Literal("[")),
Concat(Flat(Child(0)),
Style(Close, Literal("]")))),
// multi line
Concat(Style(Open, Literal("[")),
Concat(Indent(" ", None, Concat(Newline, Child(0))),
Concat(Newline,
Style(Close, Literal("]"))))),
),
many: Choice(
// single line
Concat(Style(Open, Literal("[")),
Concat(Fold(
first: Flat(Child(0)),
join: Concat(Left, Concat(Literal(", "), Flat(Right))),
),
Style(Close, Literal("]")))),
// multi line
Concat(Style(Open, Literal("[")),
Concat(
Indent(" ", None,
Concat(
Newline,
Fold(
first: Child(0),
join: Concat(Left,
Concat(Literal(","),
Concat(Newline,
Right))),
),
)
),
Concat(Newline,
Style(Close, Literal("]"))))),
),
),
),
("ObjectPair",
Choice(
// single line
Concat(Child(0),
Concat(Literal(": "),
Child(1))),
// multi line
Concat(Child(0),
Concat(Literal(":"),
Indent(" ", None, Concat(Newline, Child(1))))),
),
),
("Object",
Count(
zero:
Concat(Style(Open, Literal("{")),
Concat(FocusMark,
Style(Close, Literal("}")))),
one: Choice(
// single line
Concat(Style(Open, Literal("{")),
Concat(Flat(Child(0)),
Style(Close, Literal("}")))),
// multi line
Concat(Style(Open, Literal("{")),
Concat(Indent(" ", None, Concat(Newline, Child(0))),
Concat(Newline,
Style(Close, Literal("}"))))),
),
many: Choice(
// single line
Concat(Style(Open, Literal("{")),
Concat(Fold(
first: Flat(Child(0)),
join: Concat(Left, Concat(Literal(", "), Flat(Right))),
),
Style(Close, Literal("}")))),
// multi line
Concat(Style(Open, Literal("{")),
Concat(
Indent(" ", None,
Concat(
Newline,
Fold(
first: Child(0),
join: Concat(Left,
Concat(Literal(","),
Concat(Newline,
Right))),
),
)
),
Concat(Newline,
Style(Close, Literal("}"))))),
),
)
),
],
),
)
12 changes: 8 additions & 4 deletions src/engine/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,18 @@ pub struct Doc {
}

impl Doc {
pub fn new(s: &Storage, node: Node) -> Self {
Doc {
cursor: Location::before(s, node),
/// Constructs a new Doc. Returns `None` if the node is not a root with the root construct.
pub fn new(s: &Storage, root_node: Node) -> Option<Self> {
if !root_node.construct(s).is_root(s) || !root_node.is_root(s) {
return None;
}
Some(Doc {
cursor: Location::before_children(s, root_node).bug(),
recent: None,
undo_stack: Vec::new(),
redo_stack: Vec::new(),
bookmarks: HashMap::new(),
}
})
}

pub fn doc_ref_source<'d>(&self, s: &'d Storage) -> DocRef<'d> {
Expand Down
29 changes: 29 additions & 0 deletions src/engine/doc_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ pub struct DocSet {
}

impl DocSet {
pub fn temporary_hacky_new_for_testing() -> DocSet {
DocSet {
file_path_to_doc: HashMap::new(),
label_to_doc: HashMap::new(),
docs: Vec::new(),
}
}

pub fn new(starting_doc: Doc) -> DocSet {
let mut doc_set = DocSet {
file_path_to_doc: HashMap::new(),
Expand All @@ -74,6 +82,27 @@ impl DocSet {
doc_set
}

#[must_use]
pub fn add_doc(&mut self, doc_name: &Path, doc: Doc) -> bool {
if self.file_path_to_doc.contains_key(doc_name) {
return false;
}

let doc_index = self.insert_doc(doc);
self.file_path_to_doc.insert(doc_name.to_owned(), doc_index);
true
}

#[must_use]
pub fn set_visible_doc(&mut self, doc_name: &Path) -> bool {
if let Some(doc_index) = self.file_path_to_doc.get(doc_name) {
self.label_to_doc.insert(DocLabel::Visible, *doc_index);
true
} else {
false
}
}

pub fn visible_doc(&self) -> &Doc {
let doc_index = *self
.label_to_doc
Expand Down
Loading

0 comments on commit 0a19726

Please sign in to comment.