The entirety of phrase command parsing is in one function
This function is also responsible for setting a kind attribute on the phrase. This will make the semantic analyzer's job easier.
This commit is contained in:
parent
7bde082f36
commit
977ecba78c
@ -125,13 +125,8 @@ func (parser *ParsingOperation) parseBlockLevelPhrase (
|
|||||||
// get command
|
// get command
|
||||||
err = parser.expect(validPhraseStartTokens...)
|
err = parser.expect(validPhraseStartTokens...)
|
||||||
if err != nil { return }
|
if err != nil { return }
|
||||||
if isTokenOperator(parser.token) {
|
phrase.command, phrase.kind, err = parser.parsePhraseCommand()
|
||||||
phrase.command, err = parser.parseOperatorArgument()
|
if err != nil { return }
|
||||||
if err != nil { return }
|
|
||||||
} else {
|
|
||||||
phrase.command, err = parser.parseArgument()
|
|
||||||
if err != nil { return }
|
|
||||||
}
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
if expectRightBracket {
|
if expectRightBracket {
|
||||||
@ -258,13 +253,8 @@ func (parser *ParsingOperation) parseArgumentLevelPhrase () (
|
|||||||
// get command
|
// get command
|
||||||
err = parser.nextToken(validPhraseStartTokens...)
|
err = parser.nextToken(validPhraseStartTokens...)
|
||||||
if err != nil { return }
|
if err != nil { return }
|
||||||
if isTokenOperator(parser.token) {
|
phrase.command, phrase.kind, err = parser.parsePhraseCommand()
|
||||||
phrase.command, err = parser.parseOperatorArgument()
|
if err != nil { return }
|
||||||
if err != nil { return }
|
|
||||||
} else {
|
|
||||||
phrase.command, err = parser.parseArgument()
|
|
||||||
if err != nil { return }
|
|
||||||
}
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
// delimited
|
// delimited
|
||||||
@ -300,19 +290,45 @@ func (parser *ParsingOperation) parseArgumentLevelPhrase () (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseOperatorArgument parses an operator argument. This is only used to parse
|
// parsePhraseCommand parses the command argument of a phrase.
|
||||||
// operator phrase commands.
|
func (parser *ParsingOperation) parsePhraseCommand () (
|
||||||
func (parser *ParsingOperation) parseOperatorArgument () (
|
command Argument,
|
||||||
operator Argument,
|
kind PhraseKind,
|
||||||
err error,
|
err error,
|
||||||
) {
|
) {
|
||||||
err = parser.expect(operatorTokens...)
|
if isTokenOperator(parser.token) {
|
||||||
if err != nil { return }
|
err = parser.expect(operatorTokens...)
|
||||||
|
if err != nil { return }
|
||||||
|
|
||||||
operator.location = parser.token.Location()
|
command.location = parser.token.Location()
|
||||||
operator.kind = ArgumentKindOperator
|
command.kind = ArgumentKindOperator
|
||||||
operator.value = parser.token.Kind()
|
command.value = parser.token.Kind()
|
||||||
|
|
||||||
|
if parser.token.Is(lexer.TokenKindColon) {
|
||||||
|
kind = PhraseKindCase
|
||||||
|
} else {
|
||||||
|
kind = PhraseKindOperator
|
||||||
|
}
|
||||||
|
|
||||||
|
err = parser.nextToken()
|
||||||
|
if err != nil { return }
|
||||||
|
} else {
|
||||||
|
command, err = parser.parseArgument()
|
||||||
|
if err != nil { return }
|
||||||
|
|
||||||
|
if command.kind == ArgumentKindString {
|
||||||
|
kind = PhraseKindCallExternal
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
err = parser.nextToken()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PhraseKindSet
|
||||||
|
// PhraseKindDefer
|
||||||
|
// PhraseKindIf
|
||||||
|
// PhraseKindElseIf
|
||||||
|
// PhraseKindElse
|
||||||
|
// PhraseKindSwitch
|
||||||
|
// PhraseKindWhile
|
||||||
|
// PhraseKindFor
|
||||||
|
@ -223,6 +223,24 @@ type FaceSection struct {
|
|||||||
behaviors map[string] FaceBehavior
|
behaviors map[string] FaceBehavior
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PhraseKind determines what semantic role a phrase plays.
|
||||||
|
type PhraseKind int
|
||||||
|
|
||||||
|
const (
|
||||||
|
PhraseKindCall = iota
|
||||||
|
PhraseKindCallExternal
|
||||||
|
PhraseKindOperator
|
||||||
|
PhraseKindSet
|
||||||
|
PhraseKindDefer
|
||||||
|
PhraseKindIf
|
||||||
|
PhraseKindElseIf
|
||||||
|
PhraseKindElse
|
||||||
|
PhraseKindSwitch
|
||||||
|
PhraseKindCase
|
||||||
|
PhraseKindWhile
|
||||||
|
PhraseKindFor
|
||||||
|
)
|
||||||
|
|
||||||
// Phrase represents a function call or operator. In ARF they are the same
|
// Phrase represents a function call or operator. In ARF they are the same
|
||||||
// syntactical concept.
|
// syntactical concept.
|
||||||
type Phrase struct {
|
type Phrase struct {
|
||||||
@ -231,7 +249,9 @@ type Phrase struct {
|
|||||||
arguments []Argument
|
arguments []Argument
|
||||||
returnsTo []Argument
|
returnsTo []Argument
|
||||||
|
|
||||||
// only applicable for
|
kind PhraseKind
|
||||||
|
|
||||||
|
// only applicable for control flow phrases
|
||||||
block Block
|
block Block
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user