Skip to content

Commit

Permalink
Resolve conflicts from upstream/main
Browse files Browse the repository at this point in the history
  • Loading branch information
Xanewok committed Nov 21, 2023
2 parents 0ae2072 + 4b5f8b4 commit 861d926
Show file tree
Hide file tree
Showing 367 changed files with 2,570 additions and 3,004 deletions.
5 changes: 5 additions & 0 deletions .changeset/little-berries-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nomicfoundation/slang": patch
---

Drop List suffix from collection grammar rule names
5 changes: 5 additions & 0 deletions .changeset/purple-parents-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nomicfoundation/slang": minor
---

Remove the CST Visitor API in favor of the Cursor API
1 change: 0 additions & 1 deletion crates/codegen/parser/generator/src/code_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ impl CodeGenerator {
"parse_error.rs",
"parse_output.rs",
"text_index.rs",
"visitor.rs",
"napi/napi_cst.rs",
"napi/napi_cursor.rs",
"napi/napi_parse_error.rs",
Expand Down
8 changes: 8 additions & 0 deletions crates/codegen/parser/runtime/src/cst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ impl Node {
}
}

/// Returns a slice of the children (not all descendants) of this node.
pub fn children(&self) -> &[Node] {
match self {
Self::Rule(node) => &node.children,
Self::Token(_) => &[],
}
}

pub fn create_cursor(&self, text_offset: TextIndex) -> Cursor {
Cursor::new(self.clone(), text_offset)
}
Expand Down
13 changes: 8 additions & 5 deletions crates/codegen/parser/runtime/src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,14 @@ impl Cursor {
self.current.text_range()
}

pub fn path_rule_nodes(&self) -> Vec<Rc<RuleNode>> {
self.path
.iter()
.map(|path_element| path_element.rule_node.clone())
.collect()
/// Returns the depth of the current node in the CST, i.e. the number of ancestors.
pub fn depth(&self) -> usize {
self.path.len()
}

/// Returns an iterator over the current node's ancestors, starting from the cursor root node.
pub fn ancestors(&self) -> impl Iterator<Item = &Rc<RuleNode>> {
self.path.iter().map(|elem| &elem.rule_node)
}

/// Attempts to go to current node's next one, according to the DFS pre-order traversal.
Expand Down
1 change: 0 additions & 1 deletion crates/codegen/parser/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub(crate) mod lexer;
pub mod parse_error;
pub mod parse_output;
pub mod text_index;
pub mod visitor;

#[cfg(feature = "slang_napi_interfaces")]
pub mod napi;
3 changes: 1 addition & 2 deletions crates/codegen/parser/runtime/src/napi/napi_cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ impl Cursor {
#[napi(ts_return_type = "Array<cst.RuleNode>")]
pub fn path_rule_nodes(&self, env: Env) -> Vec<JsObject> {
self.0
.path_rule_nodes()
.iter()
.ancestors()
.map(|rust_rule_node| rust_rule_node.to_js(&env))
.collect()
}
Expand Down
1 change: 0 additions & 1 deletion crates/codegen/parser/runtime/src/templates/mod.rs.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ pub(crate) mod lexer;
pub mod parse_error;
pub mod parse_output;
pub mod text_index;
pub mod visitor;

#[cfg(feature = "slang_napi_interfaces")]
pub mod napi;
75 changes: 0 additions & 75 deletions crates/codegen/parser/runtime/src/visitor.rs
Original file line number Diff line number Diff line change
@@ -1,76 +1 @@
use std::ops::ControlFlow;
use std::rc::Rc;

use super::{cst::*, cursor::Cursor};

/// A Visitor pattern for traversing the CST.
///
/// The trait supports fallible iteration, i.e. the visitor can early return an error from the visit.
pub trait Visitor<E> {
/// Called when the [`Visitor`] enters a [`RuleNode`].
fn rule_enter(
&mut self,
_node: &Rc<RuleNode>,
_cursor: &Cursor,
) -> Result<ControlFlow<(), Step>, E> {
Ok(ControlFlow::Continue(Step::In))
}

/// Called when the [`Visitor`] exits a [`RuleNode`].
fn rule_exit(&mut self, _node: &Rc<RuleNode>, _cursor: &Cursor) -> Result<ControlFlow<()>, E> {
Ok(ControlFlow::Continue(()))
}

/// Called when the [`Visitor`] enters a [`TokenNode`].
fn token(&mut self, _node: &Rc<TokenNode>, _cursor: &Cursor) -> Result<ControlFlow<()>, E> {
Ok(ControlFlow::Continue(()))
}
}

/// Whether the [`Visitor`] should should enter the children of a [`RuleNode`] or not.
pub enum Step {
In,
Over,
}

impl Cursor {
pub fn drive_visitor<E, V: Visitor<E>>(
&mut self,
visitor: &mut V,
) -> Result<ControlFlow<()>, E> {
if self.is_completed() {
return Ok(ControlFlow::Continue(()));
}

loop {
// Node clone is cheap because it's just an enum around an Rc
match self.node() {
Node::Rule(rule_node) => {
match visitor.rule_enter(&rule_node, self)? {
ControlFlow::Break(()) => return Ok(ControlFlow::Break(())),
ControlFlow::Continue(Step::In) => {
if self.go_to_first_child() {
self.drive_visitor(visitor)?;
self.go_to_parent();
}
}
ControlFlow::Continue(Step::Over) => {}
}
if visitor.rule_exit(&rule_node, self)? == ControlFlow::Break(()) {
return Ok(ControlFlow::Break(()));
}
}

Node::Token(token_node) => {
if visitor.token(&token_node, self)? == ControlFlow::Break(()) {
return Ok(ControlFlow::Break(()));
}
}
}

if !self.go_to_next_sibling() {
return Ok(ControlFlow::Continue(()));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
unversioned:
sequence:
- optional:
reference: "SourceUnitMembersList"
reference: "SourceUnitMembers"
- optional:
reference: "EndOfFileTrivia"

- name: "SourceUnitMembersList"
- name: "SourceUnitMembers"
kind: "Parser"
unversioned:
oneOrMore:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
unversioned:
sequence:
- reference: "SolidityKeyword"
- reference: "VersionPragmaExpressionsList"
- reference: "VersionPragmaExpressions"

- name: "VersionPragmaExpressionsList"
- name: "VersionPragmaExpressions"
kind: "Parser"
unversioned:
oneOrMore:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@
open:
reference: "OpenBrace"
parser:
reference: "DeconstructionImportSymbolsList"
reference: "DeconstructionImportSymbols"
close:
reference: "CloseBrace"
- reference: "FromKeyword"
- reference: "AsciiStringLiteral"

- name: "DeconstructionImportSymbolsList"
- name: "DeconstructionImportSymbols"
kind: "Parser"
unversioned:
separatedBy:
Expand All @@ -75,7 +75,7 @@
parser:
sequence:
- reference: "UsingKeyword"
- reference: "UsingDirectivePath"
- reference: "IdentifierPath"
- reference: "ForKeyword"
- choice:
- reference: "Asterisk"
Expand All @@ -89,7 +89,7 @@
sequence:
- reference: "UsingKeyword"
- choice:
- reference: "UsingDirectivePath"
- reference: "IdentifierPath"
- reference: "UsingDirectiveDeconstruction"
- reference: "ForKeyword"
- choice:
Expand All @@ -100,11 +100,6 @@
terminator:
reference: "Semicolon"

- name: "UsingDirectivePath"
kind: "Parser"
unversioned:
reference: "IdentifierPath"

- name: "UsingDirectiveDeconstruction"
kind: "Parser"
versioned:
Expand All @@ -113,11 +108,11 @@
open:
reference: "OpenBrace"
parser:
reference: "UsingDirectiveSymbolsList"
reference: "UsingDirectiveSymbols"
close:
reference: "CloseBrace"

- name: "UsingDirectiveSymbolsList"
- name: "UsingDirectiveSymbols"
kind: "Parser"
versioned:
0.8.13:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
reference: "OpenBrace"
parser:
optional:
reference: "ContractMembersList"
reference: "ContractMembers"
close:
reference: "CloseBrace"
0.6.0:
Expand All @@ -31,7 +31,7 @@
reference: "OpenBrace"
parser:
optional:
reference: "ContractMembersList"
reference: "ContractMembers"
close:
reference: "CloseBrace"

Expand All @@ -40,9 +40,9 @@
unversioned:
sequence:
- reference: "IsKeyword"
- reference: "InheritanceTypesList"
- reference: "InheritanceTypes"

- name: "InheritanceTypesList"
- name: "InheritanceTypes"
kind: "Parser"
unversioned:
separatedBy:
Expand All @@ -59,7 +59,7 @@
- optional:
reference: "ArgumentsDeclaration"

- name: "ContractMembersList"
- name: "ContractMembers"
kind: "Parser"
unversioned:
oneOrMore:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
reference: "OpenBrace"
parser:
optional:
reference: "InterfaceMembersList"
reference: "InterfaceMembers"
close:
reference: "CloseBrace"

- name: "InterfaceMembersList"
- name: "InterfaceMembers"
kind: "Parser"
unversioned:
oneOrMore:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
reference: "OpenBrace"
parser:
optional:
reference: "LibraryMembersList"
reference: "LibraryMembers"
close:
reference: "CloseBrace"

- name: "LibraryMembersList"
- name: "LibraryMembers"
kind: "Parser"
unversioned:
oneOrMore:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
reference: "OpenBrace"
parser:
optional:
reference: "StructMembersList"
reference: "StructMembers"
close:
reference: "CloseBrace"

- name: "StructMembersList"
- name: "StructMembers"
kind: "Parser"
unversioned:
oneOrMore:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@
reference: "OpenBrace"
parser:
optional:
reference: "IdentifiersList"
reference: "EnumMembers"
close:
reference: "CloseBrace"

- name: "EnumMembers"
kind: "Parser"
unversioned:
separatedBy:
separator:
reference: "Comma"
parser:
reference: "Identifier"
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
sequence:
- reference: "TypeName"
- optional:
reference: "StateVariableAttributesList"
reference: "StateVariableAttributes"
- reference: "Identifier"
- optional:
sequence:
Expand All @@ -17,7 +17,7 @@
terminator:
reference: "Semicolon"

- name: "StateVariableAttributesList"
- name: "StateVariableAttributes"
kind: "Parser"
unversioned:
oneOrMore:
Expand Down
Loading

0 comments on commit 861d926

Please sign in to comment.