Blocks under phrases are parsed according to phrase kind

This commit is contained in:
Sasha Koshka 2022-09-03 16:28:36 -04:00
parent ac548bf438
commit c6428f6f8e

View File

@ -73,12 +73,16 @@ var validDelimitedPhraseTokens = append (
lexer.TokenKindRBracket, lexer.TokenKindRBracket,
lexer.TokenKindReturnDirection) 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. // underneath them.
var controlFlowNames = []string { var controlFlowKinds = []PhraseKind {
"if", "else", "elseif", PhraseKindIf,
"for", "while", PhraseKindElse,
"defer", PhraseKindElseIf,
PhraseKindFor,
PhraseKindWhile,
PhraseKindDefer,
PhraseKindCase,
} }
// parseBlock parses an indented block of phrases // parseBlock parses an indented block of phrases
@ -210,30 +214,15 @@ func (parser *ParsingOperation) parseBlockLevelPhrase (
if err != nil { return } if err != nil { return }
// if this is a control flow statement, parse block under it // 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 isControlFlow := false
for _, name := range controlFlowNames { for _, kind := range controlFlowKinds {
if command.trail[0] == name { if phrase.kind == kind {
isControlFlow = true isControlFlow = true
break break
} }
} }
if !isControlFlow { return } 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 it is any of those, parse the block under it // if it is any of those, parse the block under it
phrase.block, err = parser.parseBlock(indent + 1) phrase.block, err = parser.parseBlock(indent + 1)