Skip to content

Commit

Permalink
Allowed empty if/else blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaumgarten committed Dec 23, 2020
1 parent 77914e9 commit 9069d70
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 36 deletions.
26 changes: 8 additions & 18 deletions pkg/nolol/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,41 +511,31 @@ func (p *Parser) ParseIf() ast.Statement {

ret.Condition = p.This.ParseExpression()
if ret.Condition == nil {
p.ErrorExpectedExpression("as inline-if-condition")
p.ErrorExpectedExpression("as if-condition")
}

p.Expect(ast.TypeKeyword, "then")

stmt := p.This.ParseStatement()
if stmt == nil {
p.ErrorExpectedStatement("inside if-block")
}
ret.IfBlock = make([]ast.Statement, 0, 1)
ret.IfBlock = append(ret.IfBlock, stmt)

for {
stmt2 := p.This.ParseStatement()
if stmt2 == nil {
stmt := p.This.ParseStatement()
if stmt == nil {
break
}
ret.IfBlock = append(ret.IfBlock, stmt2)
ret.IfBlock = append(ret.IfBlock, stmt)
}

if p.IsCurrent(ast.TypeKeyword, "else") {
p.Advance()
stmt := p.This.ParseStatement()
if stmt == nil {
p.ErrorExpectedStatement("inside else-block")
}

ret.ElseBlock = make([]ast.Statement, 0, 1)
ret.ElseBlock = append(ret.ElseBlock, stmt)

for {
stmt2 := p.This.ParseStatement()
if stmt2 == nil {
stmt := p.This.ParseStatement()
if stmt == nil {
break
}
ret.ElseBlock = append(ret.IfBlock, stmt2)
ret.ElseBlock = append(ret.ElseBlock, stmt)
}
}

Expand Down
24 changes: 7 additions & 17 deletions pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,37 +403,27 @@ func (p *Parser) ParseIf() ast.Statement {
}

p.Expect(ast.TypeKeyword, "then")

stmt := p.This.ParseStatement()
if stmt == nil {
p.ErrorExpectedStatement("inside if-block")
}
ret.IfBlock = make([]ast.Statement, 0, 1)
ret.IfBlock = append(ret.IfBlock, stmt)

for {
stmt2 := p.This.ParseStatement()
if stmt2 == nil {
stmt := p.This.ParseStatement()
if stmt == nil {
break
}
ret.IfBlock = append(ret.IfBlock, stmt2)
ret.IfBlock = append(ret.IfBlock, stmt)
}

if p.IsCurrent(ast.TypeKeyword, "else") {
p.Advance()
stmt := p.This.ParseStatement()
if stmt == nil {
p.ErrorExpectedStatement("inside else-block")
}

ret.ElseBlock = make([]ast.Statement, 0, 1)
ret.ElseBlock = append(ret.ElseBlock, stmt)

for {
stmt2 := p.This.ParseStatement()
if stmt2 == nil {
stmt := p.This.ParseStatement()
if stmt == nil {
break
}
ret.ElseBlock = append(ret.IfBlock, stmt2)
ret.ElseBlock = append(ret.ElseBlock, stmt)
}
}

Expand Down
2 changes: 1 addition & 1 deletion vscode-yolol/src/test/diagnostics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('Should get diagnostics', () => {
it('Diagnoses errors in yolol', async () => {
const docUri = getDocUri('has_errors.yolol')
await testDiagnostics(docUri, [
{ message: 'Expected statement inside if-block. Found Token: \'then\'(Keyword)', range: toRange(1, 27, 1, 27), severity: vscode.DiagnosticSeverity.Error, source: 'parser' },
{ message: 'Expected token \'end\'(Keyword). Found Token: \'then\'(Keyword)', range: toRange(1, 27, 1, 27), severity: vscode.DiagnosticSeverity.Error, source: 'parser' },
{ message: 'Expected an assignment-operator. Found Token: \':number\'(ID)', range: toRange(4, 4, 4, 4), severity: vscode.DiagnosticSeverity.Error, source: 'parser' }
])
})
Expand Down

0 comments on commit 9069d70

Please sign in to comment.