Skip to content

Commit

Permalink
Changing parse_result to return a NonTerminalNode instead of a Node
Browse files Browse the repository at this point in the history
  • Loading branch information
beta-ziliani committed Dec 21, 2024
1 parent f257eae commit cb9d451
Show file tree
Hide file tree
Showing 26 changed files with 102 additions and 90 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::BTreeMap;
use std::rc::Rc;

use metaslang_cst::nodes::Node;
use semver::Version;

use crate::compilation::{CompilationUnit, File};
Expand Down Expand Up @@ -43,7 +44,10 @@ impl InternalCompilationBuilder {

let import_paths = self.imports.extract(parse_output.create_tree_cursor());

let file = File::new(id.clone(), parse_output.tree().clone());
let file = File::new(
id.clone(),
Node::Nonterminal(Rc::clone(&parse_output.tree())),
);
self.files.insert(id, file);

AddFileResponse { import_paths }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use crate::cst::{Cursor, Node, TextIndex};
use std::rc::Rc;

use crate::cst::{Cursor, NonterminalNode, TextIndex};
use crate::parser::ParseError;

#[derive(Clone, Debug, PartialEq)]
pub struct ParseOutput {
pub(crate) tree: Node,
pub(crate) tree: Rc<NonterminalNode>,
pub(crate) errors: Vec<ParseError>,
}

impl ParseOutput {
pub fn tree(&self) -> &Node {
&self.tree
pub fn tree(&self) -> Rc<NonterminalNode> {
Rc::clone(&self.tree)
}

pub fn errors(&self) -> &Vec<ParseError> {
Expand All @@ -22,6 +24,6 @@ impl ParseOutput {

/// Creates a cursor that starts at the root of the parse tree.
pub fn create_tree_cursor(&self) -> Cursor {
self.tree.clone().cursor_with_offset(TextIndex::ZERO)
Rc::clone(&self.tree).cursor_with_offset(TextIndex::ZERO)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::rc::Rc;

use crate::cst::{Edge, Node, TerminalKind, TerminalKindExtensions, TextIndex};
use crate::cst::{Edge, Node, NonterminalNode, TerminalKind, TerminalKindExtensions, TextIndex};
use crate::parser::lexer::Lexer;
use crate::parser::parser_support::context::ParserContext;
use crate::parser::parser_support::parser_result::{
Expand Down Expand Up @@ -85,14 +85,9 @@ where
TerminalKind::UNRECOGNIZED
};
let node = Node::terminal(kind, input.to_string());
let tree = if no_match.kind.is_none() || start.utf8 == 0 {
node
} else {
trivia_nodes.push(Edge::anonymous(node));
Node::nonterminal(no_match.kind.unwrap(), trivia_nodes)
};
trivia_nodes.push(Edge::anonymous(node));
ParseOutput {
tree,
tree: Rc::new(NonterminalNode::new(no_match.kind.unwrap(), trivia_nodes)),
errors: vec![ParseError::new(
start..start + input.into(),
no_match.expected_terminals,
Expand Down Expand Up @@ -156,17 +151,17 @@ where
));

ParseOutput {
tree: Node::nonterminal(topmost_node.kind, new_children),
tree: Rc::new(NonterminalNode::new(topmost_node.kind, new_children)),
errors,
}
} else {
let tree = Node::Nonterminal(topmost_node);
let tree = topmost_node;
let errors = stream.into_errors();

// Sanity check: Make sure that succesful parse is equivalent to not having any invalid nodes
debug_assert_eq!(
errors.is_empty(),
tree.clone()
Rc::clone(&tree)
.cursor_with_offset(TextIndex::ZERO)
.remaining_nodes()
.all(|edge| edge
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
interface parser {
use cst.{cursor, node, nonterminal-kind, text-range};
use cst.{cursor, nonterminal-node, nonterminal-kind, text-range};

/// A parser instance that can parse source code into syntax trees.
/// Each parser is configured for a specific language version and grammar.
Expand Down Expand Up @@ -32,7 +32,7 @@ interface parser {
resource parse-output {
/// Returns the root node of the parsed syntax tree.
/// Even if there are parsing errors, a partial tree will still be available.
tree: func() -> node;
tree: func() -> nonterminal-node;

/// Returns a list of all parsing errors encountered.
/// An empty list indicates successful parsing with no errors.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::wasm_crate::utils::{define_wrapper, FromFFI, IntoFFI};

mod ffi {
pub use crate::wasm_crate::bindgen::exports::nomic_foundation::slang::cst::{
Cursor, Node, TextRange,
Cursor, NonterminalNode, TextRange,
};
pub use crate::wasm_crate::bindgen::exports::nomic_foundation::slang::parser::{
Guest, GuestParseError, GuestParseOutput, GuestParser, NonterminalKind, ParseError,
Expand Down Expand Up @@ -70,8 +70,8 @@ define_wrapper! { ParseError {
//================================================

define_wrapper! { ParseOutput {
fn tree(&self) -> ffi::Node {
self._borrow_ffi().tree().clone()._into_ffi()
fn tree(&self) -> ffi::NonterminalNode {
self._borrow_ffi().tree()._into_ffi()
}

fn errors(&self) -> Vec<ffi::ParseError> {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/metaslang/cst/generated/public_api.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 13 additions & 7 deletions crates/metaslang/cst/src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ pub struct Edge<T: KindTypes> {
pub node: Node<T>,
}

impl<T: KindTypes> NonterminalNode<T> {
pub fn new(kind: T::NonterminalKind, children: Vec<Edge<T>>) -> Self {
let text_len = children.iter().map(|edge| edge.text_len()).sum();

NonterminalNode {
kind,
text_len,
children,
}
}
}

impl<T: KindTypes> Edge<T> {
/// Creates an anonymous node (without a label).
pub fn anonymous(node: Node<T>) -> Self {
Expand All @@ -81,13 +93,7 @@ impl<T: KindTypes> std::ops::Deref for Edge<T> {

impl<T: KindTypes> Node<T> {
pub fn nonterminal(kind: T::NonterminalKind, children: Vec<Edge<T>>) -> Self {
let text_len = children.iter().map(|edge| edge.text_len()).sum();

Self::Nonterminal(Rc::new(NonterminalNode {
kind,
text_len,
children,
}))
Self::Nonterminal(Rc::new(NonterminalNode::new(kind, children)))
}

pub fn terminal(kind: T::TerminalKind, text: String) -> Self {
Expand Down
4 changes: 4 additions & 0 deletions crates/solidity/outputs/cargo/crate/generated/public_api.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ pub fn add_built_ins(
let parser = Parser::create(version)?;
let parse_output = parser.parse(Parser::ROOT_KIND, source);

let built_ins_cursor = transform(parse_output.tree()).cursor_with_offset(TextIndex::ZERO);
let built_ins_cursor =
transform(&Node::Nonterminal(parse_output.tree())).cursor_with_offset(TextIndex::ZERO);

binding_graph.add_system_file("built_ins.sol", built_ins_cursor);
Ok(())
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ fn using_the_cursor() -> Result<()> {

let identifiers: Vec<_> = parse_output
.tree()
.clone()
.descendants()
.filter(|edge| edge.is_terminal_with_kind(TerminalKind::Identifier))
.map(|identifier| identifier.unparse())
Expand All @@ -106,7 +105,6 @@ fn using_the_cursor() -> Result<()> {

let identifiers: Vec<_> = parse_output
.tree()
.clone()
.descendants()
.filter(|edge| edge.label == Some(EdgeLabel::Name))
.filter(|edge| edge.is_terminal_with_kind(TerminalKind::Identifier))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ fn using_the_parser() -> Result<()> {
// --8<-- [end:assert-is-valid]

// --8<-- [start:inspect-tree]
let tree = parse_output.tree();
let contract = parse_output.tree();

let contract = tree.as_nonterminal().unwrap();
assert_eq!(contract.kind, NonterminalKind::ContractDefinition);
assert_eq!(contract.children.len(), 7);

Expand Down
1 change: 0 additions & 1 deletion crates/solidity/outputs/cargo/tests/src/trivia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ fn compare_end_of_lines(input: &str, expected: &[&str]) -> Result<()> {

let actual = output
.tree()
.clone()
.descendants()
.filter(|edge| edge.is_terminal_with_kind(TerminalKind::EndOfLine))
.map(|eol| eol.unparse())
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cb9d451

Please sign in to comment.