Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RealA10N committed Aug 10, 2024
1 parent 30f85b4 commit c1fa21e
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 18 deletions.
4 changes: 2 additions & 2 deletions parse/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (p BlockParser[NodeT]) String() string {
}

func (p BlockParser[NodeT]) Parse(v *TokenView) (block BlockNode[NodeT], err ParsingError) {
leftCurly, err := v.ConsumeToken(lex.LeftCurlyBraceToken)
leftCurly, err := v.ConsumeTokenIgnoreSeparator(lex.LeftCurlyBraceToken)

if err != nil {
return
Expand All @@ -46,7 +46,7 @@ func (p BlockParser[NodeT]) Parse(v *TokenView) (block BlockNode[NodeT], err Par
block.Start = leftCurly.View.Start
block.Nodes = ParseMany(p.Parser, v)

rightCurly, err := v.ConsumeToken(lex.RightCurlyBraceToken)
rightCurly, err := v.ConsumeTokenIgnoreSeparator(lex.RightCurlyBraceToken)
if err != nil {
return
}
Expand Down
33 changes: 21 additions & 12 deletions parse/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,39 +76,48 @@ func TestSingleFunction(t *testing.T) {
}

func TestFileParserTwoFunctionsNoExtraSeparator(t *testing.T) {
src := `function @first =
ret
src := `function @first = { ret }
function @second =
ret`
{
ret
}`

expected := `function @first =
expected := `function @first = {
ret
}
function @second =
function @second = {
ret
}
`
testFormattedFile(t, src, expected)
}

func TestFileWithLabels(t *testing.T) {
src := `function $32 @fib $i32 %n =
jle %n $32 #1 .return
src := `
function $32 @fib $i32 %n =
{
jle %n $32 #1 .return
%n = dec %n
%a = call @fib %n
%n = dec %n
%b = call @fib %n
%b = call @fib %n
%n = add %a %b
.return
ret %n`
expected := `function $32 @fib $i32 %n =
.return ret %n }
`

expected := `function $32 @fib $i32 %n = {
jle %n $32 #1 .return
%n = dec %n
%a = call @fib %n
%n = dec %n
%b = call @fib %n
%n = add %a %b
.return ret %n
.return
ret %n
}
`
testFormattedFile(t, src, expected)
}
Expand Down
2 changes: 1 addition & 1 deletion parse/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (FunctionParser) String() string {
}

func (FunctionParser) parseFunctionKeyword(v *TokenView, node *FunctionNode) ParsingError {
kw, err := v.ConsumeToken(lex.FunctionKeywordToken)
kw, err := v.ConsumeTokenIgnoreSeparator(lex.FunctionKeywordToken)
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions parse/instruction.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ func (InstructionParser) parseOperator(v *TokenView, node *InstructionNode) Pars
//
// > Lbl* (Reg+ Eql)? Opr Arg+ !Arg
func (p InstructionParser) Parse(v *TokenView) (node InstructionNode, err ParsingError) {
v.ConsumeManyTokens(lex.SeparatorToken)
node.Labels, _ = ParseManyIgnoreSeparators(p.LabelParser, v)
node.Targets = ParseMany(p.RegisterParser, v)

Expand All @@ -107,6 +106,6 @@ func (p InstructionParser) Parse(v *TokenView) (node InstructionNode, err Parsin
}

node.Arguments = ParseMany(p.ArgumentParser, v)
err = v.ConsumeAtLeastTokens(1, lex.SeparatorToken)
return node, err
v.ConsumeManyTokens(lex.SeparatorToken)
return node, nil
}
1 change: 1 addition & 0 deletions parse/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func ParseManyIgnoreSeparators[Node any](
p Parser[Node],
v *TokenView,
) (nodes []Node, err ParsingError) {
v.ConsumeManyTokens(lex.SeparatorToken)
for {
var node Node
node, err = p.Parse(v)
Expand Down
8 changes: 8 additions & 0 deletions parse/token_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ func (v *TokenView) ConsumeManyTokens(
}
}

// Consume a token, but ignore any separator tokens that come before it.
func (v *TokenView) ConsumeTokenIgnoreSeparator(
expectedTypes ...lex.TokenType,
) (lex.Token, ParsingError) {
v.ConsumeManyTokens(lex.SeparatorToken)
return v.ConsumeToken(expectedTypes...)
}

// Consume as many tokens as possible greedly, until we recieve an error.
//
// If the number of tokens consumed is strictly less than the provided number,
Expand Down

0 comments on commit c1fa21e

Please sign in to comment.