allow more curly brace styles

This commit is contained in:
Irmen de Jong 2023-06-27 01:59:22 +02:00
parent eab63ecc6c
commit 0a83b51e00
9 changed files with 78 additions and 25 deletions

View File

@ -151,7 +151,7 @@ class TestModuleImporter: FunSpec({
shouldThrow<ParseError> { act() }.let { shouldThrow<ParseError> { act() }.let {
it.position.file shouldBe SourceCode.relative(srcPath).toString() it.position.file shouldBe SourceCode.relative(srcPath).toString()
withClue("line; should be 1-based") { it.position.line shouldBe 2 } withClue("line; should be 1-based") { it.position.line shouldBe 2 }
withClue("startCol; should be 0-based") { it.position.startCol shouldBe 6 } withClue("startCol; should be 0-based") { it.position.startCol shouldBe 4 }
withClue("endCol; should be 0-based") { it.position.endCol shouldBe 6 } withClue("endCol; should be 0-based") { it.position.endCol shouldBe 6 }
} }
} }
@ -171,7 +171,7 @@ class TestModuleImporter: FunSpec({
shouldThrow<ParseError> { act() }.let { shouldThrow<ParseError> { act() }.let {
it.position.file shouldBe SourceCode.relative(imported).toString() it.position.file shouldBe SourceCode.relative(imported).toString()
withClue("line; should be 1-based") { it.position.line shouldBe 2 } withClue("line; should be 1-based") { it.position.line shouldBe 2 }
withClue("startCol; should be 0-based") { it.position.startCol shouldBe 6 } withClue("startCol; should be 0-based") { it.position.startCol shouldBe 4 }
withClue("endCol; should be 0-based") { it.position.endCol shouldBe 6 } withClue("endCol; should be 0-based") { it.position.endCol shouldBe 6 }
} }
} }
@ -231,7 +231,7 @@ class TestModuleImporter: FunSpec({
importer.importImplicitLibraryModule(srcPath.nameWithoutExtension) }.let { importer.importImplicitLibraryModule(srcPath.nameWithoutExtension) }.let {
it.position.file shouldBe SourceCode.relative(srcPath).toString() it.position.file shouldBe SourceCode.relative(srcPath).toString()
withClue("line; should be 1-based") { it.position.line shouldBe 2 } withClue("line; should be 1-based") { it.position.line shouldBe 2 }
withClue("startCol; should be 0-based") { it.position.startCol shouldBe 6 } withClue("startCol; should be 0-based") { it.position.startCol shouldBe 4 }
withClue("endCol; should be 0-based") { it.position.endCol shouldBe 6 } withClue("endCol; should be 0-based") { it.position.endCol shouldBe 6 }
} }
} }
@ -253,7 +253,7 @@ class TestModuleImporter: FunSpec({
act() }.let { act() }.let {
it.position.file shouldBe SourceCode.relative(imported).toString() it.position.file shouldBe SourceCode.relative(imported).toString()
withClue("line; should be 1-based") { it.position.line shouldBe 2 } withClue("line; should be 1-based") { it.position.line shouldBe 2 }
withClue("startCol; should be 0-based") { it.position.startCol shouldBe 6 } withClue("startCol; should be 0-based") { it.position.startCol shouldBe 4 }
withClue("endCol; should be 0-based") { it.position.endCol shouldBe 6 } withClue("endCol; should be 0-based") { it.position.endCol shouldBe 6 }
} }
} }

View File

@ -274,7 +274,7 @@ class TestProg8Parser: FunSpec( {
val path = assumeReadableFile(fixturesDir, "ast_file_with_syntax_error.p8") val path = assumeReadableFile(fixturesDir, "ast_file_with_syntax_error.p8")
val e = shouldThrow<ParseError> { parseModule(SourceCode.File(path)) } val e = shouldThrow<ParseError> { parseModule(SourceCode.File(path)) }
assertPosition(e.position, SourceCode.relative(path).toString(), 2, 6) assertPosition(e.position, SourceCode.relative(path).toString(), 2, 4)
} }
test("of Module parsed from a string") { test("of Module parsed from a string") {

View File

@ -275,5 +275,50 @@ main {
errors.errors[0] shouldContain "invalid statement in unroll loop" errors.errors[0] shouldContain "invalid statement in unroll loop"
errors.errors[1] shouldContain "invalid statement in unroll loop" errors.errors[1] shouldContain "invalid statement in unroll loop"
} }
test("various curly brace styles") {
val src="""
main
{
sub start()
{
ubyte variable=55
when variable
{
33 -> cx16.r0++
else -> cx16.r1++
}
if variable {
cx16.r0++
} else {
cx16.r1++
}
if variable { cx16.r0++ }
else { cx16.r1++ }
if variable
{
cx16.r0++
}
else
{
cx16.r1++
}
other.othersub()
}
}
other {
sub othersub() {
cx16.r0++
}
}"""
compileText(VMTarget(), optimize=false, src, writeAssembly=false) shouldNotBe null
}
}) })

View File

@ -1,2 +1,2 @@
; test expects the following 2nd (!) line: ; test expects the following 2nd (!) line:
bad { } ; -> missing EOL at '}' (ie: *after* the '{') bad bad bad { } } }

View File

@ -1,2 +1,2 @@
; test expects the following 2nd (!) line: ; test expects the following 2nd (!) line:
bad { } ; -> missing EOL at '}' (ie: *after* the '{') bad bad bad { } } }

View File

@ -621,8 +621,6 @@ The syntax is::
return X * 3 return X * 3
} }
The open curly brace must immediately follow the subroutine result specification on the same line,
and can have nothing following it. The close curly brace must be on its own line as well.
The parameters is a (possibly empty) comma separated list of "<datatype> <parametername>" pairs specifying the input parameters. The parameters is a (possibly empty) comma separated list of "<datatype> <parametername>" pairs specifying the input parameters.
The return type has to be specified if the subroutine returns a value. The return type has to be specified if the subroutine returns a value.

View File

@ -1,11 +1,6 @@
TODO TODO
==== ====
- also allow this?
if variable { txt.print("yes") }
else { txt.print("no") }
- is it possible to allow the curly brace to be on the next line instead of requiring it to follow on the same line?
... ...

View File

@ -1,24 +1,39 @@
%import textio main
%zeropage basicsafe {
main {
sub start() sub start()
{ {
ubyte variable=55 ubyte variable=55
when variable when variable
{ {
33 -> txt.print("33") 33 -> cx16.r0++
else -> cx16.r1++
} }
if variable {
cx16.r0++
} else {
cx16.r1++
}
if variable { cx16.r0++ }
else { cx16.r1++ }
if variable if variable
{ {
txt.print("yes") cx16.r0++
} }
else else
{ {
txt.print("no") cx16.r1++
} }
other.othersub()
} }
} }
other {
sub othersub() {
cx16.r0++
}
}

View File

@ -66,7 +66,7 @@ ARRAYSIG :
// Note: the parser may see *several* consecutive EOLs - this happens when EOL and comments are interleaved (see #47) // Note: the parser may see *several* consecutive EOLs - this happens when EOL and comments are interleaved (see #47)
module: EOL* ((directive | block) (EOL+ (directive | block))*)? EOL* EOF; module: EOL* ((directive | block) (EOL+ (directive | block))*)? EOL* EOF;
block: identifier integerliteral? '{' EOL (block_statement | EOL)* '}'; block: identifier integerliteral? EOL? '{' EOL? (block_statement | EOL)* '}';
block_statement: block_statement:
directive directive
@ -245,7 +245,7 @@ subroutine :
sub_return_part : '->' datatype ; sub_return_part : '->' datatype ;
statement_block : statement_block :
'{' EOL '{' EOL?
(statement | EOL) * (statement | EOL) *
'}' '}'
; ;
@ -296,6 +296,6 @@ repeatloop: 'repeat' expression? EOL? (statement | statement_block) ;
unrollloop: 'unroll' integerliteral? EOL? (statement | statement_block) ; unrollloop: 'unroll' integerliteral? EOL? (statement | statement_block) ;
whenstmt: 'when' expression EOL? '{' EOL (when_choice | EOL) * '}' EOL? ; whenstmt: 'when' expression EOL? '{' EOL? (when_choice | EOL) * '}' EOL? ;
when_choice: (expression_list | 'else' ) '->' (statement | statement_block ) ; when_choice: (expression_list | 'else' ) '->' (statement | statement_block ) ;