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 11, 2024
1 parent 8983d3c commit 10dd4f7
Show file tree
Hide file tree
Showing 19 changed files with 77 additions and 81 deletions.
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(Debug, PartialEq)]
pub struct ParseOutput {
pub(crate) parse_tree: Node,
pub(crate) parse_tree: Rc<NonterminalNode>,
pub(crate) errors: Vec<ParseError>,
}

impl ParseOutput {
pub fn tree(&self) -> Node {
self.parse_tree.clone()
pub fn tree(&self) -> Rc<NonterminalNode> {
Rc::clone(&self.parse_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.parse_tree.clone().cursor_with_offset(TextIndex::ZERO)
Rc::clone(&self.parse_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 {
parse_tree: tree,
parse_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,18 +151,17 @@ where
));

ParseOutput {
parse_tree: Node::nonterminal(topmost_node.kind, new_children),
parse_tree: Rc::new(NonterminalNode::new(topmost_node.kind, new_children)),
errors,
}
} else {
let parse_tree = Node::Nonterminal(topmost_node);
let parse_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(),
parse_tree
.clone()
Rc::clone(&parse_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 @@ -36,7 +36,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::bindings::exports::nomic_foundation::slang::cst::{
Cursor, Node, TextRange,
Cursor, NonterminalNode, TextRange,
};
pub use crate::wasm_crate::bindings::exports::nomic_foundation::slang::parser::{
Guest, GuestParseError, GuestParseOutput, GuestParser, NonterminalKind, ParseError,
Expand Down Expand Up @@ -77,7 +77,7 @@ define_wrapper! { ParseError {
//================================================

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

Expand Down
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

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.

7 changes: 4 additions & 3 deletions crates/solidity/outputs/cargo/tests/src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::Arc;
use anyhow::Result;
use semver::Version;
use slang_solidity::bindings::{self, Bindings};
use slang_solidity::cst::TextIndex;
use slang_solidity::cst::{Node, TextIndex};
use slang_solidity::parser::Parser;
use slang_solidity::transform_built_ins_node;

Expand All @@ -20,8 +20,9 @@ pub fn create_bindings(version: &Version) -> Result<Bindings> {
"built-ins parse without errors"
);

let built_ins_cursor = transform_built_ins_node(&built_ins_parse_output.tree())
.cursor_with_offset(TextIndex::ZERO);
let built_ins_cursor =
transform_built_ins_node(&Node::Nonterminal(built_ins_parse_output.tree()))
.cursor_with_offset(TextIndex::ZERO);

bindings.add_system_file("built_ins.sol", built_ins_cursor);
Ok(bindings)
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 parse_tree = parse_output.tree();
let contract = parse_output.tree();

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

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.

6 changes: 3 additions & 3 deletions crates/solidity/testing/sanctuary/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use itertools::Itertools;
use metaslang_bindings::PathResolver;
use semver::Version;
use slang_solidity::bindings::Bindings;
use slang_solidity::cst::{Cursor, NonterminalKind, TextIndex, TextRange};
use slang_solidity::cst::{Cursor, Node, NonterminalKind, TextIndex, TextRange};
use slang_solidity::diagnostic::{Diagnostic, Severity};
use slang_solidity::parser::{ParseOutput, Parser};
use slang_solidity::{bindings, transform_built_ins_node};
Expand Down Expand Up @@ -218,8 +218,8 @@ fn create_bindings(version: &Version, source_id: &str, output: &ParseOutput) ->
bindings::get_built_ins(version),
)
.tree();
let built_ins_cursor =
transform_built_ins_node(&built_ins_tree).cursor_with_offset(TextIndex::ZERO);
let built_ins_cursor = transform_built_ins_node(&Node::Nonterminal(built_ins_tree))
.cursor_with_offset(TextIndex::ZERO);

bindings.add_system_file("built_ins.sol", built_ins_cursor);
bindings.add_user_file(source_id, output.create_tree_cursor());
Expand Down

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

Loading

0 comments on commit 10dd4f7

Please sign in to comment.