Skip to content

Commit

Permalink
improved lexer test
Browse files Browse the repository at this point in the history
  • Loading branch information
Egbert.Voigt authored and Egbert.Voigt committed Mar 25, 2020
1 parent 36a6987 commit 55079e2
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 34 deletions.
1 change: 1 addition & 0 deletions codegen/templates/D.stg
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,7 @@ package <lexerFile.genPackage>;
import antlr.v4.runtime.Lexer;
import antlr.v4.runtime.CharStream;
import antlr.v4.runtime.InterfaceRuleContext;
import antlr.v4.runtime.RuleContext;
import antlr.v4.runtime.RuntimeMetaData;
import antlr.v4.runtime.Token;
import antlr.v4.runtime.TokenStream;
Expand Down
6 changes: 3 additions & 3 deletions source/antlr/v4/runtime/dfa/DFA.d
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
* Copyright (c) 2012-2020 The ANTLR Project. All rights reserved.
* Use of this file is governed by the BSD 3-clause license that
* can be found in the LICENSE.txt file in the project root.
*/
Expand Down Expand Up @@ -189,14 +189,14 @@ class DFA
}

DFASerializer serializer = new DFASerializer(this, vocabulary);
return serializer.toString();
return serializer.toString;
}

public string toLexerString()
{
if (s0 is null) return "";
DFASerializer serializer = new LexerDFASerializer(this);
return serializer.toString();
return serializer.toString;
}

}
Expand Down
30 changes: 3 additions & 27 deletions source/antlr/v4/runtime/dfa/DFASerializer.d
Original file line number Diff line number Diff line change
@@ -1,31 +1,7 @@
/*
* [The "BSD license"]
* Copyright (c) 2012 Terence Parr
* Copyright (c) 2012 Sam Harwell
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* Copyright (c) 2012-2020 The ANTLR Project. All rights reserved.
* Use of this file is governed by the BSD 3-clause license that
* can be found in the LICENSE.txt file in the project root.
*/

module antlr.v4.runtime.dfa.DFASerializer;
Expand Down
3 changes: 2 additions & 1 deletion source/antlr/v4/runtime/dfa/LexerDFASerializer.d
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class LexerDFASerializer : DFASerializer
*/
public override string getEdgeLabel(int i)
{
return "'" ~ to!string(i) ~ "'";
import std.format : format;
return format!"'%s'"(cast(char)i);
}

}
2 changes: 1 addition & 1 deletion test/D.test.stg
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Text() ::= "this.getText.get!(string)"

ValEquals(a,b) ::= <%<a>==<b>%>

TextEquals(a) ::= <%this.getText.get!(string).equals("<a>")%>
TextEquals(a) ::= <%this.getText.get!(string) == "<a>"%>

PlusText(a) ::= <%"<a>" ~ this.getText.get!(string)%>

Expand Down
6 changes: 4 additions & 2 deletions test/d/BaseDTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -892,11 +892,13 @@ protected void writeParserTestFile(String parserName, String lexerName,

protected void writeLexerTestFile(String lexerName, boolean showDFA) {
ST outputFileST = new ST(
"import std.stdio : File, writefln;\n"
"import std.stdio : File, write, writefln;\n"
+ "\n"
+ "import antlr.v4.runtime.Token;\n"
+ "import antlr.v4.runtime.ANTLRInputStream;\n"
+ "import antlr.v4.runtime.CommonTokenStream;\n"
+ "import antlr.v4.runtime.Lexer;\n"
+ "import antlr.v4.runtime.atn.LexerATNSimulator;\n"
+ "import <lexerName>;\n"
+ "\n"
+ "int main(string[] args) {\n"
Expand All @@ -907,7 +909,7 @@ protected void writeLexerTestFile(String lexerName, boolean showDFA) {
+ " tokens.fill();\n"
+ " foreach (token; tokens.getTokens)\n"
+ " writefln!\"%s\"(token);\n"
+ (showDFA ? " std::cout \\<\\< lexer.getInterpreter\\<atn::LexerATNSimulator>()->getDFA(Lexer::DEFAULT_MODE).toLexerString();\n" : "\n")
+ (showDFA ? " write((cast(LexerATNSimulator)lexer.getInterpreter).getDFA(Lexer.DEFAULT_MODE).toLexerString);\n" : "\n")
+ " return 0;\n"
+ "}\n");
outputFileST.add("lexerName", lexerName);
Expand Down

0 comments on commit 55079e2

Please sign in to comment.