From e323b91a32ecff1473d9330605d85ad5f3684e99 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Thu, 6 Oct 2022 22:36:32 +0800 Subject: [PATCH] correct grammar (ref #20199) and add check for grammar.txt (#20494) * correct grammar; ref #20199 * add check for keeping grammar.txt up-to-date * add nimTestGrammar --- compiler/parser.nim | 35 ++++++++++++++++++++++++++--------- doc/grammar.txt | 3 +-- tests/compiler/tgrammar.nim | 7 +++++++ 3 files changed, 34 insertions(+), 11 deletions(-) create mode 100644 tests/compiler/tgrammar.nim diff --git a/compiler/parser.nim b/compiler/parser.nim index 9f48976658..2c514ee74f 100644 --- a/compiler/parser.nim +++ b/compiler/parser.nim @@ -16,20 +16,37 @@ # In fact the grammar is generated from this file: -when isMainModule: +when isMainModule or defined(nimTestGrammar): # Leave a note in grammar.txt that it is generated: #| # This file is generated by compiler/parser.nim. - import pegs + import std/pegs when defined(nimPreviewSlimSystem): import std/syncio - var outp = open("doc/grammar.txt", fmWrite) - for line in lines("compiler/parser.nim"): - if line =~ peg" \s* '#| ' {.*}": - outp.write matches[0], "\L" - outp.close - import ".." / tools / grammar_nanny - checkGrammarFile() + proc writeGrammarFile(x: string) = + var outp = open(x, fmWrite) + for line in lines("compiler/parser.nim"): + if line =~ peg" \s* '#| ' {.*}": + outp.write matches[0], "\L" + outp.close + + when defined(nimTestGrammar): + import std/os + from ../testament/lib/stdtest/specialpaths import buildDir + const newGrammarText = buildDir / "grammar.txt" + + if not dirExists(buildDir): + createDir(buildDir) + + writeGrammarFile(newGrammarText) + + proc checkSameGrammar*() = + doAssert sameFileContent(newGrammarText, "doc/grammar.txt"), + "execute 'nim r compiler.nim' to keep grammar.txt up-to-date" + else: + writeGrammarFile("doc/grammar.txt") + import ".." / tools / grammar_nanny + checkGrammarFile() import llstream, lexer, idents, strutils, ast, msgs, options, lineinfos, diff --git a/doc/grammar.txt b/doc/grammar.txt index 29a4300b6e..63ce4503c5 100644 --- a/doc/grammar.txt +++ b/doc/grammar.txt @@ -175,8 +175,7 @@ objectDecl = 'object' ('of' typeDesc)? COMMENT? objectPart conceptParam = ('var' | 'out')? symbol conceptDecl = 'concept' conceptParam ^* ',' (pragma)? ('of' typeDesc ^* ',')? &IND{>} stmt -typeDef = identWithPragmaDot genericParamList? '=' optInd typeDefAux - indAndComment? / identVisDot genericParamList? pragma '=' optInd typeDefAux +typeDef = identVisDot genericParamList? pragma '=' optInd typeDefAux indAndComment? varTuple = '(' optInd identWithPragma ^+ comma optPar ')' '=' optInd expr colonBody = colcom stmt postExprBlocks? diff --git a/tests/compiler/tgrammar.nim b/tests/compiler/tgrammar.nim new file mode 100644 index 0000000000..772d0f0ccb --- /dev/null +++ b/tests/compiler/tgrammar.nim @@ -0,0 +1,7 @@ +discard """ + matrix: "-d:nimTestGrammar" +""" + +import compiler/parser + +checkSameGrammar()