Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reinstate not_followed_by and add to DecimalLiteral also remove _ in dsl keywords. #575

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions crates/codegen/grammar/src/dsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,22 +281,22 @@ macro_rules! slang_parser {
)
};

(( $b:tt delimited_by $o:tt and $c:tt )) => {
(( $b:tt delimited by $o:tt and $c:tt )) => {
$crate::ParserDefinitionNode::DelimitedBy(
Box::new(slang_parser!($o)),
Box::new(slang_parser!($b)),
Box::new(slang_parser!($c)),
slang_location!()
)
};
(( $b:tt terminated_by $t:tt )) => {
(( $b:tt terminated by $t:tt )) => {
$crate::ParserDefinitionNode::TerminatedBy(
Box::new(slang_parser!($b)),
Box::new(slang_parser!($t)),
slang_location!()
)
};
(( $b:tt separated_by $s:tt )) => {
(( $b:tt separated by $s:tt )) => {
$crate::ParserDefinitionNode::SeparatedBy(
Box::new(slang_parser!($b)),
Box::new(slang_parser!($s)),
Expand Down Expand Up @@ -497,6 +497,15 @@ macro_rules! slang_scanner {
)
};

(( $b:tt not followed by $nla:tt )) => {
$crate::ScannerDefinitionNode::NotFollowedBy(
Box::new(slang_scanner!($b)),
Box::new(slang_scanner!($nla)),
slang_location!()
)
};


(( $first:tt | $($rest:tt)|+ )) => {
$crate::ScannerDefinitionNode::Choice(
vec![slang_scanner!($first), $(slang_scanner!($rest)),+],
Expand Down
6 changes: 6 additions & 0 deletions crates/codegen/grammar/src/scanner_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub enum ScannerDefinitionNode {
Sequence(Vec<Self>, SourceLocation),
Choice(Vec<Self>, SourceLocation),
NoneOf(String, SourceLocation),
NotFollowedBy(Box<Self>, Box<Self>, SourceLocation),
CharRange(char, char, SourceLocation),
Literal(String, SourceLocation),
ScannerDefinition(ScannerDefinitionRef, SourceLocation),
Expand All @@ -56,6 +57,11 @@ impl Visitable for ScannerDefinitionNode {
}
}

Self::NotFollowedBy(node, lookahead, _) => {
node.accept_visitor(visitor);
lookahead.accept_visitor(visitor);
}

Self::NoneOf(_, _)
| Self::CharRange(_, _, _)
| Self::Literal(_, _)
Expand Down
6 changes: 6 additions & 0 deletions crates/codegen/parser/generator/src/scanner_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ impl ScannerDefinitionNodeExtensions for ScannerDefinitionNode {
quote! { scan_none_of!(stream, #(#chars),*) }
}

ScannerDefinitionNode::NotFollowedBy(node, lookahead, _) => {
let scanner = node.to_scanner_code();
let negative_lookahead_scanner = lookahead.to_scanner_code();
quote! { scan_not_followed_by!(stream, #scanner, #negative_lookahead_scanner) }
}

ScannerDefinitionNode::Sequence(nodes, _) => {
let scanners = nodes
.iter()
Expand Down
18 changes: 0 additions & 18 deletions crates/codegen/parser/runtime/src/support/scanner_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,24 +108,6 @@ macro_rules! scan_optional {
}};
}

#[allow(unused_macros)]
macro_rules! scan_difference {
($stream:ident, $minuend:expr, $subtrahend:expr) => {{
let start = $stream.position();
($minuend)
&& ({
let end = $stream.position();
$stream.set_position(start);
if ($subtrahend) && (end == $stream.position()) {
false
} else {
$stream.set_position(end);
true
}
})
}};
}

#[allow(unused_macros)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this allow still needed?

macro_rules! scan_not_followed_by {
($stream:ident, $scanner:expr, $not_followed_by:expr) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I like how delimited by tokens improve legibility of the DSL, maybe changing it to

($stream:ident, $scanner:expr, not followed by: $not_followed_by:expr)

would improve legibility of scan_not_followed_by macro calls here?

Expand Down
176 changes: 99 additions & 77 deletions crates/solidity/inputs/language/src/dsl.rs

Large diffs are not rendered by default.

161 changes: 98 additions & 63 deletions crates/solidity/outputs/cargo/crate/src/generated/language.rs

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.

11 changes: 10 additions & 1 deletion crates/solidity/outputs/cargo/tests/src/scanner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use slang_solidity::{

#[test]
fn test_next_token() {
let version = Version::parse("0.4.11").unwrap();
let version = Version::parse("0.8.0").unwrap();
let language = Language::new(version).unwrap();

for (s, k) in &[
Expand All @@ -18,9 +18,18 @@ fn test_next_token() {
("+=", PlusEqual),
("1", DecimalLiteral),
("\n", EndOfLine),
("unicode'abc'", UnicodeStringLiteral),
("unicode'abc'ZZ", Identifier), // TODO: This needs to be further checked against solc
("hex'abcd'", HexStringLiteral),
("hex'abcd'ZZz", HexKeyword), // TODO: This needs to be further checked against solc
("// single line\n", SingleLineComment),
("/* multi-line\n comment */ blah", MultilineComment),
("/* multi-line comment **/ blah", MultilineComment),
] {
assert_eq!(language.scan(LexicalContext::Default, s), Some(*k));
}

assert_eq!(language.scan(LexicalContext::Default, "0ZZ"), None);
assert_eq!(language.scan(LexicalContext::Default, "0xabZZ"), None);
assert_eq!(language.scan(LexicalContext::Default, "'abc'ZZ"), None);
}
Loading