From 6e1684630c7e1af7c3f441acbed3acccc543ba2e Mon Sep 17 00:00:00 2001 From: NeilBrown Date: Wed, 5 Jun 2019 18:21:18 +1000 Subject: [PATCH] oceani: redo parsing of blank lines. I've been puzzling how best to write a grammar to handle blank lines and option line-breaks well. The "OptNL" approach didn't work, and "Newlines" only sometimes works. I won't try to explain all the logic here, but I do plan to write a blog post about it soon. Signed-off-by: NeilBrown --- csrc/oceani.mdc | 169 +++++++++++++++++++++++++----------------------- 1 file changed, 89 insertions(+), 80 deletions(-) diff --git a/csrc/oceani.mdc b/csrc/oceani.mdc index 86b3e3c..f4d8517 100644 --- a/csrc/oceani.mdc +++ b/csrc/oceani.mdc @@ -2013,22 +2013,29 @@ function will be needed. } } }$ + $void + Open -> { + | NEWLINE Open + Close -> } + | NEWLINE Close $*fieldlist - FieldBlock -> Open SimpleFieldList Close ${ $0 = $<2; }$ - | Open Newlines SimpleFieldList Close ${ $0 = $<3; }$ + FieldBlock -> Open FieldList Close ${ $0 = $<2; }$ + | Open SimpleFieldList } ${ $0 = $<2; }$ | : FieldList ${ $0 = $<2; }$ - FieldList -> Field NEWLINE ${ $0 = $<1; }$ - | FieldList NEWLINE ${ $0 = $<1; }$ - | FieldList Field NEWLINE ${ + FieldList -> SimpleFieldList NEWLINE ${ $0 = $<1; }$ + | FieldList SimpleFieldList NEWLINE ${ $2->prev = $<1; $0 = $<2; }$ - SimpleFieldList -> Field ; ${ $0 = $<1; }$ - | SimpleFieldList Field ; ${ - $2->prev = $<1; - $0 = $<2; + SimpleFieldList -> Field ${ $0 = $<1; }$ + | SimpleFieldList ; Field ${ + $3->prev = $<1; + $0 = $<3; + }$ + | SimpleFieldList ; ${ + $0 = $<1; }$ Field -> IDENTIFIER : Type = Expression ${ { @@ -3018,44 +3025,43 @@ is in-place. ###### Grammar $void - OptNL -> Newlines - | - Newlines -> NEWLINE | Newlines NEWLINE $*binode - Open -> { - | NEWLINE { - Close -> } - | NEWLINE } Block -> Open Statementlist Close ${ $0 = $<2; }$ - | Open Newlines Statementlist Close ${ $0 = $<3; }$ | Open SimpleStatements } ${ $0 = reorder_bilist($<2); }$ - | Open Newlines SimpleStatements } ${ $0 = reorder_bilist($<3); }$ - | : Statementlist ${ $0 = $<2; }$ | : SimpleStatements ${ $0 = reorder_bilist($<2); }$ + | : Statementlist ${ $0 = $<2; }$ Statementlist -> ComplexStatements ${ $0 = reorder_bilist($<1); }$ ComplexStatements -> ComplexStatements ComplexStatement ${ - $0 = new(binode); - $0->op = Block; - $0->left = $<1; - $0->right = $<2; + if ($2 == NULL) { + $0 = $<1; + } else { + $0 = new(binode); + $0->op = Block; + $0->left = $<1; + $0->right = $<2; + } }$ - | ComplexStatements NEWLINE ${ $0 = $<1; }$ | ComplexStatement ${ - $0 = new(binode); - $0->op = Block; - $0->left = NULL; - $0->right = $<1; + if ($1 == NULL) { + $0 = NULL; + } else { + $0 = new(binode); + $0->op = Block; + $0->left = NULL; + $0->right = $<1; + } }$ $*exec ComplexStatement -> SimpleStatements NEWLINE ${ $0 = reorder_bilist($<1); }$ + | Newlines ${ $0 = NULL; }$ ## ComplexStatement Grammar $*binode @@ -3529,24 +3535,32 @@ defined. $*cond_statement // both ForThen and Whilepart open scopes, and CondSuffix only // closes one - so in the first branch here we have another to close. - CondStatement -> ForThen WhilePart CondSuffix ${ + CondStatement -> forPart ThenPart WhilePart CondSuffix ${ + $0 = $<4; + $0->forpart = $<1; + $0->thenpart = $<2; + $0->condpart = $3.condpart; $3.condpart = NULL; + $0->dopart = $3.dopart; $3.dopart = NULL; + var_block_close(config2context(config), CloseSequential); + }$ + | forPart WhilePart CondSuffix ${ $0 = $<3; - $0->forpart = $1.forpart; $1.forpart = NULL; - $0->thenpart = $1.thenpart; $1.thenpart = NULL; + $0->forpart = $<1; + $0->thenpart = NULL; $0->condpart = $2.condpart; $2.condpart = NULL; $0->dopart = $2.dopart; $2.dopart = NULL; var_block_close(config2context(config), CloseSequential); }$ - | WhilePart CondSuffix ${ + | whilePart CondSuffix ${ $0 = $<2; $0->condpart = $1.condpart; $1.condpart = NULL; $0->dopart = $1.dopart; $1.dopart = NULL; }$ - | SwitchPart CondSuffix ${ + | switchPart CondSuffix ${ $0 = $<2; $0->condpart = $<1; }$ - | IfPart IfSuffix ${ + | ifPart IfSuffix ${ $0 = $<2; $0->condpart = $1.condpart; $1.condpart = NULL; $0->thenpart = $1.thenpart; $1.thenpart = NULL; @@ -3566,14 +3580,11 @@ defined. $0->casepart = $<1; }$ + $void + Case -> case + | NEWLINE Case $*casepart - CasePart -> Newlines case Expression OpenScope Block ${ - $0 = calloc(1,sizeof(struct casepart)); - $0->value = $<3; - $0->action = $<5; - var_block_close(config2context(config), CloseParallel); - }$ - | case Expression OpenScope Block ${ + CasePart -> Case Expression OpenScope Block ${ $0 = calloc(1,sizeof(struct casepart)); $0->value = $<2; $0->action = $<4; @@ -3581,92 +3592,91 @@ defined. }$ $*cond_statement - IfSuffix -> Newlines ${ $0 = new(cond_statement); }$ - | Newlines else OpenScope Block ${ - $0 = new(cond_statement); - $0->elsepart = $<4; - var_block_close(config2context(config), CloseElse); - }$ + IfSuffix -> ${ $0 = new(cond_statement); }$ + | NEWLINE IfSuffix ${ $0 = $<2; }$ | else OpenScope Block ${ $0 = new(cond_statement); $0->elsepart = $<3; var_block_close(config2context(config), CloseElse); }$ - | Newlines else OpenScope CondStatement ${ - $0 = new(cond_statement); - $0->elsepart = $<4; - var_block_close(config2context(config), CloseElse); - }$ | else OpenScope CondStatement ${ $0 = new(cond_statement); $0->elsepart = $<3; var_block_close(config2context(config), CloseElse); }$ + $void + Then -> then + | NEWLINE Then + While -> while + | NEWLINE While + Do -> do + | NEWLINE Do $*exec // These scopes are closed in CondSuffix - ForPart -> for OpenScope SimpleStatements ${ + forPart -> for OpenScope SimpleStatements ${ $0 = reorder_bilist($<3); }$ | for OpenScope Block ${ $0 = $<3; }$ - ThenPart -> then OpenScope SimpleStatements ${ + ThenPart -> Then OpenScope SimpleStatements ${ $0 = reorder_bilist($<3); var_block_close(config2context(config), CloseSequential); }$ - | then OpenScope Block ${ + | Then OpenScope Block ${ $0 = $<3; var_block_close(config2context(config), CloseSequential); }$ - ThenPartNL -> ThenPart OptNL ${ - $0 = $<1; - }$ - // This scope is closed in CondSuffix - WhileHead -> while OpenScope Block ${ + WhileHead -> While OpenScope Block ${ $0 = $<3; }$ - - $cond_statement - ForThen -> ForPart OptNL ThenPartNL ${ - $0.forpart = $<1; - $0.thenpart = $<3; - }$ - | ForPart OptNL ${ - $0.forpart = $<1; + whileHead -> while OpenScope Block ${ + $0 = $<3; }$ + $cond_statement // This scope is closed in CondSuffix - WhilePart -> while OpenScope Expression Block ${ + whilePart -> while OpenScope Expression Block ${ $0.type = Xcond_statement; $0.condpart = $<3; $0.dopart = $<4; }$ - | WhileHead OptNL do Block ${ + | whileHead Do Block ${ $0.type = Xcond_statement; $0.condpart = $<1; + $0.dopart = $<3; + }$ + WhilePart -> While OpenScope Expression Block ${ + $0.type = Xcond_statement; + $0.condpart = $<3; $0.dopart = $<4; }$ + | WhileHead Do Block ${ + $0.type = Xcond_statement; + $0.condpart = $<1; + $0.dopart = $<3; + }$ - IfPart -> if OpenScope Expression OpenScope Block ${ + ifPart -> if OpenScope Expression OpenScope Block ${ $0.type = Xcond_statement; $0.condpart = $<3; $0.thenpart = $<5; var_block_close(config2context(config), CloseParallel); }$ - | if OpenScope Block OptNL then OpenScope Block ${ + | if OpenScope Block Then OpenScope Block ${ $0.type = Xcond_statement; $0.condpart = $<3; - $0.thenpart = $<7; + $0.thenpart = $<6; var_block_close(config2context(config), CloseParallel); }$ $*exec // This scope is closed in CondSuffix - SwitchPart -> switch OpenScope Expression ${ + switchPart -> switch OpenScope Expression ${ $0 = $<3; }$ | switch OpenScope Block ${ @@ -3946,19 +3956,18 @@ searching through for the Nth constant for decreasing N. ###### top level grammar DeclareConstant -> const Open ConstList Close - | const Open Newlines ConstList Close | const Open SimpleConstList } - | const Open Newlines SimpleConstList } | const : ConstList - | const SimpleConstList + | const SimpleConstList NEWLINE ConstList -> ComplexConsts + | NEWLINE ConstList ComplexConsts -> ComplexConst ComplexConsts | ComplexConst ComplexConst -> SimpleConstList NEWLINE - SimpleConstList -> Const ; SimpleConstList + SimpleConstList -> SimpleConstList ; Const | Const - | Const ; SimpleConstList ; + | SimpleConstList ; $*type CType -> Type ${ $0 = $<1; }$ @@ -4054,7 +4063,7 @@ analysis is a bit more interesting at this level. } }$ $*binode - Program -> program OpenScope Varlist Block OptNL ${ + Program -> program OpenScope Varlist Block ${ $0 = new(binode); $0->op = Program; $0->left = reorder_bilist($<3); -- 2.43.0