Merge pull request #277 from vmx/relax-extern-match

Allow match definition if no custom tokens are defined
This commit is contained in:
Niko Matsakis 2018-01-24 05:18:32 -05:00 committed by GitHub
commit 038ea15f04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 10 deletions

1
.gitignore vendored
View File

@ -17,6 +17,7 @@ doc/whitespace/src/parser.rs
lalrpop-test/src/error.rs
lalrpop-test/src/error_issue_113.rs
lalrpop-test/src/error_issue_278.rs
lalrpop-test/src/error_issue_261.rs
lalrpop-test/src/error_recovery.rs
lalrpop-test/src/error_recovery_pull_182.rs
lalrpop-test/src/error_recovery_issue_240.rs

View File

@ -0,0 +1,25 @@
use std::str::FromStr;
use lalrpop_util::ParseError;
grammar;
extern {
type Error = &'static str;
}
pub Value: f64 = {
<n:Float> => n,
<n:Int> => n as f64,
};
Int: u64 = r"\d+"
=>? u64::from_str(<>).map_err(|_| ParseError::User{ error: "invalid int" });
Float: f64 = r"\d+(\.\d+)?(e-?\d+)?"
=>? f64::from_str(<>).map_err(|_| ParseError::User{ error: "invalid float" });
match {
r"\d+(\.\d+)?(e-?\d+)?"
} else {
_
}

View File

@ -66,11 +66,13 @@ impl<'grammar> Validator<'grammar> {
"multiple match definitions are not permitted");
}
// We may want to allow a limited extern to coexist with match in the future
// Only error if a custom lexer is specified, having a custom types is ok
if let Some(d) = self.extern_token {
return_err!(
d.span,
"extern and match definitions are mutually exclusive");
if d.enum_token.is_some() {
return_err!(
d.span,
"extern (with custom tokens) and match definitions are mutually exclusive");
}
}
// Ensure that the catch all is final item of final block
@ -94,11 +96,13 @@ impl<'grammar> Validator<'grammar> {
"multiple extern definitions are not permitted");
}
// We may want to allow a limited extern to coexist with match in the future
// Only error if a custom lexer is specified, having a custom types is ok
if let Some(d) = self.match_token {
return_err!(
d.span,
"match and extern definitions are mutually exclusive");
if data.enum_token.is_some() {
return_err!(
d.span,
"match and extern (with custom tokens) definitions are mutually exclusive");
}
}
let allowed_names = vec![intern(LOCATION), intern(ERROR)];

View File

@ -82,7 +82,7 @@ fn multiple_match_token() {
#[test]
fn match_after_extern_token() {
check_err(
r#"match and extern definitions are mutually exclusive"#,
r#"match and extern \(with custom tokens\) definitions are mutually exclusive"#,
r#"grammar; extern { enum Tok { } } match { _ }"#,
r#" ~~~~~ "#);
}
@ -90,7 +90,7 @@ fn match_after_extern_token() {
#[test]
fn extern_after_match_token() {
check_err(
r#"extern and match definitions are mutually exclusive"#,
r#"extern \(with custom tokens\) and match definitions are mutually exclusive"#,
r#"grammar; match { _ } extern { enum Tok { } }"#,
r#" ~~~~~~ "#);
}