* correct grammar; ref #20199 * add check for keeping grammar.txt up-to-date * add nimTestGrammar
This commit is contained in:
parent
114acff98d
commit
e323b91a32
3 changed files with 34 additions and 11 deletions
|
|
@ -16,20 +16,37 @@
|
||||||
|
|
||||||
|
|
||||||
# In fact the grammar is generated from this file:
|
# 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:
|
# Leave a note in grammar.txt that it is generated:
|
||||||
#| # This file is generated by compiler/parser.nim.
|
#| # This file is generated by compiler/parser.nim.
|
||||||
import pegs
|
import std/pegs
|
||||||
when defined(nimPreviewSlimSystem):
|
when defined(nimPreviewSlimSystem):
|
||||||
import std/syncio
|
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
|
proc writeGrammarFile(x: string) =
|
||||||
checkGrammarFile()
|
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
|
import
|
||||||
llstream, lexer, idents, strutils, ast, msgs, options, lineinfos,
|
llstream, lexer, idents, strutils, ast, msgs, options, lineinfos,
|
||||||
|
|
|
||||||
|
|
@ -175,8 +175,7 @@ objectDecl = 'object' ('of' typeDesc)? COMMENT? objectPart
|
||||||
conceptParam = ('var' | 'out')? symbol
|
conceptParam = ('var' | 'out')? symbol
|
||||||
conceptDecl = 'concept' conceptParam ^* ',' (pragma)? ('of' typeDesc ^* ',')?
|
conceptDecl = 'concept' conceptParam ^* ',' (pragma)? ('of' typeDesc ^* ',')?
|
||||||
&IND{>} stmt
|
&IND{>} stmt
|
||||||
typeDef = identWithPragmaDot genericParamList? '=' optInd typeDefAux
|
typeDef = identVisDot genericParamList? pragma '=' optInd typeDefAux
|
||||||
indAndComment? / identVisDot genericParamList? pragma '=' optInd typeDefAux
|
|
||||||
indAndComment?
|
indAndComment?
|
||||||
varTuple = '(' optInd identWithPragma ^+ comma optPar ')' '=' optInd expr
|
varTuple = '(' optInd identWithPragma ^+ comma optPar ')' '=' optInd expr
|
||||||
colonBody = colcom stmt postExprBlocks?
|
colonBody = colcom stmt postExprBlocks?
|
||||||
|
|
|
||||||
7
tests/compiler/tgrammar.nim
Normal file
7
tests/compiler/tgrammar.nim
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
discard """
|
||||||
|
matrix: "-d:nimTestGrammar"
|
||||||
|
"""
|
||||||
|
|
||||||
|
import compiler/parser
|
||||||
|
|
||||||
|
checkSameGrammar()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue