Adapt the parser to parse := instead of =

This commit is contained in:
Olivier Goffart 2020-05-25 10:46:00 +02:00
parent 5228f3695d
commit dd756ef112
5 changed files with 26 additions and 16 deletions

View file

@ -21,7 +21,16 @@ fn fill_token_vec(stream: TokenStream, vec: &mut Vec<parser::Token>) {
TokenTree::Punct(p) => { TokenTree::Punct(p) => {
let kind = match p.as_char() { let kind = match p.as_char() {
':' => SyntaxKind::Colon, ':' => SyntaxKind::Colon,
'=' => SyntaxKind::Equal, '=' => {
if let Some(last) = vec.last_mut() {
if last.kind == SyntaxKind::Colon {
last.kind = SyntaxKind::ColonEqual;
last.text = ":=".into();
continue;
}
}
SyntaxKind::Equal
}
';' => SyntaxKind::Semicolon, ';' => SyntaxKind::Semicolon,
'!' => SyntaxKind::Bang, '!' => SyntaxKind::Bang,
_ => SyntaxKind::Error, _ => SyntaxKind::Error,

View file

@ -1,6 +1,6 @@
// Using a macro for now. But there could be others ways to do that // Using a macro for now. But there could be others ways to do that
sixtyfps::sixtyfps! { sixtyfps::sixtyfps! {
SuperSimple = Rectangle { SuperSimple := Rectangle {
color: white; color: white;
Rectangle { Rectangle {

View file

@ -3,7 +3,7 @@ use wasm_bindgen::prelude::*;
// Using a macro for now. But there could be others ways to do that // Using a macro for now. But there could be others ways to do that
sixtyfps::sixtyfps! { sixtyfps::sixtyfps! {
SuperSimple = Rectangle { SuperSimple := Rectangle {
color: white; color: white;
Rectangle { Rectangle {

View file

@ -73,6 +73,7 @@ declare_token_kind! {
RBrace -> r"\}", RBrace -> r"\}",
LParent -> r"\(", LParent -> r"\(",
RParent -> r"\)", RParent -> r"\)",
ColonEqual -> ":=",
FatArrow -> r"=>", FatArrow -> r"=>",
Equal -> r"=", Equal -> r"=",
Colon -> r":", Colon -> r":",

View file

@ -3,15 +3,15 @@ use super::prelude::*;
#[cfg_attr(test, parser_test)] #[cfg_attr(test, parser_test)]
/// ```test /// ```test
/// Type = Base { } /// Type := Base { }
/// Type = Base { SubElement { } } /// Type := Base { SubElement { } }
/// component Comp = Base {} Type = Base {} /// component Comp := Base {} Type := Base {}
/// ``` /// ```
pub fn parse_document(p: &mut impl Parser) -> bool { pub fn parse_document(p: &mut impl Parser) -> bool {
let mut p = p.start_node(SyntaxKind::Document); let mut p = p.start_node(SyntaxKind::Document);
loop { loop {
if p.peek().as_str() == "component" && p.nth(1) != SyntaxKind::Equal { if p.peek().as_str() == "component" && p.nth(1) != SyntaxKind::ColonEqual {
p.expect(SyntaxKind::Identifier); p.expect(SyntaxKind::Identifier);
} }
@ -27,13 +27,13 @@ pub fn parse_document(p: &mut impl Parser) -> bool {
#[cfg_attr(test, parser_test)] #[cfg_attr(test, parser_test)]
/// ```test /// ```test
/// Type = Base { } /// Type := Base { }
/// Type = Base { prop: value; } /// Type := Base { prop: value; }
/// Type = Base { SubElement { } } /// Type := Base { SubElement { } }
/// ``` /// ```
pub fn parse_component(p: &mut impl Parser) -> bool { pub fn parse_component(p: &mut impl Parser) -> bool {
let mut p = p.start_node(SyntaxKind::Component); let mut p = p.start_node(SyntaxKind::Component);
if !(p.expect(SyntaxKind::Identifier) && p.expect(SyntaxKind::Equal)) { if !(p.expect(SyntaxKind::Identifier) && p.expect(SyntaxKind::ColonEqual)) {
return false; return false;
} }
@ -62,7 +62,7 @@ pub fn parse_element(p: &mut impl Parser) -> bool {
#[cfg_attr(test, parser_test)] #[cfg_attr(test, parser_test)]
/// ```test /// ```test
/// property1: value; property2: value; /// property1: value; property2: value;
/// sub = Sub { } /// sub := Sub { }
/// for xx in model: Sub {} /// for xx in model: Sub {}
/// clicked => {} /// clicked => {}
/// signal foobar; /// signal foobar;
@ -74,7 +74,7 @@ fn parse_element_content(p: &mut impl Parser) {
SyntaxKind::Eof => return, SyntaxKind::Eof => return,
SyntaxKind::Identifier => match p.nth(1) { SyntaxKind::Identifier => match p.nth(1) {
SyntaxKind::Colon => parse_property_binding(&mut *p), SyntaxKind::Colon => parse_property_binding(&mut *p),
SyntaxKind::Equal | SyntaxKind::LBrace => parse_sub_element(&mut *p), SyntaxKind::ColonEqual | SyntaxKind::LBrace => parse_sub_element(&mut *p),
SyntaxKind::FatArrow => parse_signal_connection(&mut *p), SyntaxKind::FatArrow => parse_signal_connection(&mut *p),
SyntaxKind::Identifier if p.peek().as_str() == "for" => { SyntaxKind::Identifier if p.peek().as_str() == "for" => {
parse_repeated_element(&mut *p); parse_repeated_element(&mut *p);
@ -98,15 +98,15 @@ fn parse_element_content(p: &mut impl Parser) {
#[cfg_attr(test, parser_test)] #[cfg_attr(test, parser_test)]
/// ```test /// ```test
/// Bar {} /// Bar {}
/// foo = Bar {} /// foo := Bar {}
/// Bar { x : y ; } /// Bar { x : y ; }
/// ``` /// ```
/// Must consume at least one token /// Must consume at least one token
fn parse_sub_element(p: &mut impl Parser) { fn parse_sub_element(p: &mut impl Parser) {
let mut p = p.start_node(SyntaxKind::SubElement); let mut p = p.start_node(SyntaxKind::SubElement);
if p.nth(1) == SyntaxKind::Equal { if p.nth(1) == SyntaxKind::ColonEqual {
assert!(p.expect(SyntaxKind::Identifier)); assert!(p.expect(SyntaxKind::Identifier));
p.expect(SyntaxKind::Equal); p.expect(SyntaxKind::ColonEqual);
} }
parse_element(&mut *p); parse_element(&mut *p);
} }