From c6428f6f8ebfcba1f496b82bc806168636217bd8 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sat, 3 Sep 2022 16:28:36 -0400 Subject: [PATCH] Blocks under phrases are parsed according to phrase kind --- parser/phrase.go | 41 +++++++++++++++-------------------------- 1 file changed, 15 insertions(+), 26 deletions(-) diff --git a/parser/phrase.go b/parser/phrase.go index 413daf2..80bc2a4 100644 --- a/parser/phrase.go +++ b/parser/phrase.go @@ -73,12 +73,16 @@ var validDelimitedPhraseTokens = append ( lexer.TokenKindRBracket, lexer.TokenKindReturnDirection) -// controlFlowNames contains a list of all command names that must have a block +// controlFlowKinds contains a list of all phrase kinds that must have a block // underneath them. -var controlFlowNames = []string { - "if", "else", "elseif", - "for", "while", - "defer", +var controlFlowKinds = []PhraseKind { + PhraseKindIf, + PhraseKindElse, + PhraseKindElseIf, + PhraseKindFor, + PhraseKindWhile, + PhraseKindDefer, + PhraseKindCase, } // parseBlock parses an indented block of phrases @@ -210,29 +214,14 @@ func (parser *ParsingOperation) parseBlockLevelPhrase ( if err != nil { return } // if this is a control flow statement, parse block under it - if phrase.command.kind == ArgumentKindIdentifier { - // perhaps it is a normal control flow statement? - command := phrase.command.value.(Identifier) - if len(command.trail) != 1 { return } - - isControlFlow := false - for _, name := range controlFlowNames { - if command.trail[0] == name { - isControlFlow = true - break - } + isControlFlow := false + for _, kind := range controlFlowKinds { + if phrase.kind == kind { + isControlFlow = true + break } - - if !isControlFlow { return } - - } else if phrase.command.kind == ArgumentKindOperator { - // perhaps it is a switch case? - command := phrase.command.value.(lexer.TokenKind) - if command != lexer.TokenKindColon { return } - - } else { - return } + if !isControlFlow { return } // if it is any of those, parse the block under it phrase.block, err = parser.parseBlock(indent + 1)