From 97d36965cbb30108340727a982539e67dafea92d Mon Sep 17 00:00:00 2001 From: Ryo Nihei Date: Thu, 14 Apr 2022 02:11:06 +0900 Subject: Add tests for compiler --- spec/parser.go | 16 ++++++++++------ spec/parser_test.go | 5 +++++ spec/syntax_error.go | 1 + 3 files changed, 16 insertions(+), 6 deletions(-) (limited to 'spec') 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 @@ -183,6 +183,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: !;`, 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") -- cgit v1.2.3