From 577aa5e72a37a2c3f4cf4115c68bd7998435b813 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Wed, 22 Apr 2020 19:26:46 -0400 Subject: [PATCH] DEV: Experimental parsing support Shape Projection syntax support for #55 --- integration_tests/types_test.ucg | 12 ++++++++++++ src/parse/mod.rs | 25 +++++++++++++++++++++++-- src/tokenizer/mod.rs | 5 +++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/integration_tests/types_test.ucg b/integration_tests/types_test.ucg index 2574378..5c213bd 100644 --- a/integration_tests/types_test.ucg +++ b/integration_tests/types_test.ucg @@ -110,4 +110,16 @@ assert t.ok{ assert t.ok{ test = str(false) == "false", desc = "You can cast true into a string", +}; + +let mystr::"" = "a string"; + +let struct_shape = { + foo :: "" = "", +}; + +let func_shape = func(arg :: "", arg2 :: "") => arg + arg2 :: ""; + +let module_shape = module{self={}} => (result :: {}) { + let result :: {} = self; }; \ No newline at end of file diff --git a/src/parse/mod.rs b/src/parse/mod.rs index 2cbc237..96b9f90 100644 --- a/src/parse/mod.rs +++ b/src/parse/mod.rs @@ -216,6 +216,7 @@ make_fn!( do_each!( field => wrap_err!(either!(match_type!(BOOLEAN), match_type!(BAREWORD), match_type!(STR)), "Field names must be a bareword or a string."), + _ => optional!(shape_suffix), _ => must!(punct!("=")), value => must!(expression), (field, value) @@ -288,6 +289,15 @@ make_fn!( ) ); +make_fn!( + shape_suffix, Value>, + do_each!( + _ => punct!("::"), + shape => value, + (dbg!(shape)) + ) +); + fn value_to_expression(v: Value) -> Expression { Expression::Simple(v) } @@ -370,7 +380,11 @@ fn tuple_to_func<'a>( make_fn!( arglist, Vec>, - separated!(punct!(","), symbol) + separated!(punct!(","), do_each!( + sym => symbol, + _ => optional!(shape_suffix), + (sym) + )) ); fn module_expression(input: SliceIter) -> Result, Expression> { @@ -385,7 +399,12 @@ fn module_expression(input: SliceIter) -> Result, Expres out_expr => optional!( do_each!( _ => punct!("("), - expr => must!(expression), + expr => must!(do_each!( + expr => expression, + _ => optional!(shape_suffix), + (expr) + ) + ), _ => must!(punct!(")")), (expr) ) @@ -418,6 +437,7 @@ fn func_expression(input: SliceIter) -> Result, Expressi _ => must!(punct!(")")), _ => must!(punct!("=>")), map => trace_parse!(expression), + _ => optional!(shape_suffix), (pos, arglist, map) ); match parsed { @@ -788,6 +808,7 @@ make_fn!( do_each!( pos => pos, name => wrap_err!(match_type!(BAREWORD), "Expected name for binding"), + _ => optional!(trace_parse!(shape_suffix)), _ => punct!("="), val => trace_parse!(wrap_err!(expression, "Expected Expression to bind")), _ => punct!(";"), diff --git a/src/tokenizer/mod.rs b/src/tokenizer/mod.rs index 0b5fc5b..8d301a8 100644 --- a/src/tokenizer/mod.rs +++ b/src/tokenizer/mod.rs @@ -271,6 +271,10 @@ make_fn!(semicolontok, do_text_token_tok!(TokenType::PUNCT, ";") ); +make_fn!(doublecolontok, + do_text_token_tok!(TokenType::PUNCT, "::") +); + make_fn!(colontok, do_text_token_tok!(TokenType::PUNCT, ":") ); @@ -459,6 +463,7 @@ fn token<'a>(input: OffsetStrIter<'a>) -> Result, Token> { fatcommatok, // Note fatcommatok must come before equaltok equaltok, semicolontok, + doublecolontok, colontok, leftsquarebracket, rightsquarebracket,