aboutsummaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorRyo Nihei <nihei.dev@gmail.com>2022-04-14 02:11:06 +0900
committerRyo Nihei <nihei.dev@gmail.com>2022-04-15 21:27:19 +0900
commit97d36965cbb30108340727a982539e67dafea92d (patch)
tree8b08cfb995dc8eb219c1a8bdc753133113316b92 /spec
parentMove compiler tests from driver package to grammar package (diff)
downloadcotia-97d36965cbb30108340727a982539e67dafea92d.tar.gz
cotia-97d36965cbb30108340727a982539e67dafea92d.tar.xz
Add tests for compiler
Diffstat (limited to 'spec')
-rw-r--r--spec/parser.go16
-rw-r--r--spec/parser_test.go5
-rw-r--r--spec/syntax_error.go1
3 files changed, 16 insertions, 6 deletions
diff --git a/spec/parser.go b/spec/parser.go
index 2d16614..a1d23f0 100644
--- a/spec/parser.go
+++ b/spec/parser.go
@@ -56,6 +56,7 @@ type DirectiveNode struct {
type ParameterNode struct {
ID string
+ Pattern string
String string
Expansion bool
Pos Position
@@ -197,19 +198,17 @@ func (p *parser) parseMetaData() *DirectiveNode {
mdPos := p.lastTok.pos
if !p.consume(tokenKindID) {
- raiseSyntaxError(p.pos.Row, synErrNoProductionName)
+ raiseSyntaxError(p.pos.Row, synErrNoMDName)
}
name := p.lastTok.text
var params []*ParameterNode
for {
- if !p.consume(tokenKindID) {
+ param := p.parseParameter()
+ if param == nil {
break
}
- params = append(params, &ParameterNode{
- ID: p.lastTok.text,
- Pos: p.lastTok.pos,
- })
+ params = append(params, param)
}
return &DirectiveNode{
@@ -463,6 +462,11 @@ func (p *parser) parseParameter() *ParameterNode {
ID: p.lastTok.text,
Pos: p.lastTok.pos,
}
+ case p.consume(tokenKindTerminalPattern):
+ param = &ParameterNode{
+ Pattern: p.lastTok.text,
+ Pos: p.lastTok.pos,
+ }
case p.consume(tokenKindStringLiteral):
param = &ParameterNode{
String: p.lastTok.text,
diff --git a/spec/parser_test.go b/spec/parser_test.go
index 0772dae..2a44acd 100644
--- a/spec/parser_test.go
+++ b/spec/parser_test.go
@@ -184,6 +184,11 @@ c: ;
},
},
{
+ caption: "`fragment` is a reserved word",
+ src: `fragment: 'fragment';`,
+ synErr: synErrNoProductionName,
+ },
+ {
caption: "when a source contains an unknown token, the parser raises a syntax error",
src: `a: !;`,
synErr: synErrInvalidToken,
diff --git a/spec/syntax_error.go b/spec/syntax_error.go
index c4e6594..fdf9c40 100644
--- a/spec/syntax_error.go
+++ b/spec/syntax_error.go
@@ -25,6 +25,7 @@ var (
// syntax errors
synErrInvalidToken = newSyntaxError("invalid token")
+ synErrNoMDName = newSyntaxError("a metadata name is missing")
synErrNoProductionName = newSyntaxError("a production name is missing")
synErrNoColon = newSyntaxError("the colon must precede alternatives")
synErrNoSemicolon = newSyntaxError("the semicolon is missing at the last of an alternative")