forked from jasder/antlr
add LogManager, add InputSymbolType()
[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9413]
This commit is contained in:
parent
18e8a853e7
commit
4b4a7d9c7a
|
@ -1,11 +0,0 @@
|
|||
package org.antlr.v4.runtime;
|
||||
|
||||
public class TreeParserRuleContext<T> extends ParserRuleContext<T> {
|
||||
public TreeParserRuleContext() {
|
||||
super();
|
||||
}
|
||||
|
||||
public TreeParserRuleContext(RuleContext parent, int stateNumber) {
|
||||
super(parent, stateNumber);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,121 @@
|
|||
/*
|
||||
[The "BSD license"]
|
||||
Copyright (c) 2011 Terence Parr
|
||||
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.
|
||||
*/
|
||||
|
||||
package org.antlr.v4.runtime.misc;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
public class LogManager {
|
||||
protected static class Record {
|
||||
long timestamp;
|
||||
StackTraceElement location;
|
||||
String component;
|
||||
String msg;
|
||||
public Record() {
|
||||
timestamp = System.currentTimeMillis();
|
||||
location = new Throwable().getStackTrace()[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date(timestamp)));
|
||||
buf.append(" ");
|
||||
buf.append(component);
|
||||
buf.append(" ");
|
||||
buf.append(location.getFileName());
|
||||
buf.append(":");
|
||||
buf.append(location.getLineNumber());
|
||||
buf.append(" ");
|
||||
buf.append(msg);
|
||||
return buf.toString();
|
||||
}
|
||||
}
|
||||
|
||||
protected List<Record> records;
|
||||
|
||||
public void log(@Nullable String component, String msg) {
|
||||
Record r = new Record();
|
||||
r.component = component;
|
||||
r.msg = msg;
|
||||
if ( records==null ) {
|
||||
records = new ArrayList<Record>();
|
||||
}
|
||||
records.add(r);
|
||||
}
|
||||
|
||||
public void log(String msg) { log(null, msg); }
|
||||
|
||||
public void save(String filename) throws IOException {
|
||||
FileWriter fw = new FileWriter(filename);
|
||||
BufferedWriter bw = new BufferedWriter(fw);
|
||||
try {
|
||||
bw.write(toString());
|
||||
}
|
||||
finally {
|
||||
bw.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void save() throws IOException {
|
||||
String dir = System.getProperty("java.io.tmpdir");
|
||||
dir = ".";
|
||||
String defaultFilename =
|
||||
dir + "/antlr-" +
|
||||
new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss").format(new Date()) + ".log";
|
||||
System.out.println(defaultFilename);
|
||||
save(defaultFilename);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if ( records==null ) return "";
|
||||
String nl = System.getProperty("line.separator");
|
||||
StringBuilder buf = new StringBuilder();
|
||||
for (Record r : records) {
|
||||
buf.append(r);
|
||||
buf.append(nl);
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
LogManager mgr = new LogManager();
|
||||
mgr.log("atn", "test msg");
|
||||
mgr.log("dfa", "test msg 2");
|
||||
System.out.println(mgr);
|
||||
mgr.save();
|
||||
}
|
||||
}
|
|
@ -36,7 +36,7 @@ ListenerFile(file, header) ::= <<
|
|||
import org.antlr.v4.runtime.tree.*;
|
||||
import org.antlr.v4.runtime.Token;
|
||||
|
||||
public interface <file.grammarName>Listener extends ParseTreeListener\<<TokenLabelType()>\> {
|
||||
public interface <file.grammarName>Listener extends ParseTreeListener\<<InputSymbolType()>\> {
|
||||
<file.listenerNames:{lname |
|
||||
void enterRule(<file.parserName>.<lname>Context ctx);
|
||||
void exitRule(<file.parserName>.<lname>Context ctx);}; separator="\n">
|
||||
|
@ -54,9 +54,9 @@ public class Blank<file.grammarName>Listener implements <file.grammarName>Listen
|
|||
@Override public void enterRule(<file.parserName>.<lname>Context ctx) { \}
|
||||
@Override public void exitRule(<file.parserName>.<lname>Context ctx) { \}}; separator="\n">
|
||||
|
||||
@Override public void enterEveryRule(ParserRuleContext\<<TokenLabelType()> > ctx) { }
|
||||
@Override public void exitEveryRule(ParserRuleContext\<<TokenLabelType()> > ctx) { }
|
||||
@Override public void visitTerminal(<TokenLabelType()> symbol) { }
|
||||
@Override public void enterEveryRule(ParserRuleContext\<<InputSymbolType()> > ctx) { }
|
||||
@Override public void exitEveryRule(ParserRuleContext\<<InputSymbolType()> > ctx) { }
|
||||
@Override public void visitTerminal(<InputSymbolType()> symbol) { }
|
||||
}
|
||||
>>
|
||||
|
||||
|
@ -461,10 +461,10 @@ if (!(<chunks>)) throw new FailedPredicateException(this, <if(p.msg)><p.msg><els
|
|||
>>
|
||||
|
||||
ParserASTExtensionMembers(s) ::= <<
|
||||
public ASTAdaptor\<<ASTLabelType()> > _adaptor = (ASTAdaptor)new CommonASTAdaptor();
|
||||
public ASTAdaptor\<<ASTLabelType()>\> _adaptor = (ASTAdaptor)new CommonASTAdaptor();
|
||||
>>
|
||||
|
||||
ParserASTContextInterface(s) ::= "ASTContext\<<ASTLabelType()> >"
|
||||
ParserASTContextInterface(s) ::= "ASTContext\<<ASTLabelType()>>"
|
||||
ParserASTTreeFieldDecl(s) ::= "<ASTLabelType()> tree"
|
||||
ParserASTContextMembers(s) ::= <<
|
||||
@Override public <ASTLabelType()> getTree() { return tree; }
|
||||
|
@ -488,6 +488,7 @@ LexerSetAttr(s,rhsChunks) ::= "<s.name> = <rhsChunks>;"
|
|||
|
||||
ASTLabelType() ::= "<file.ASTLabelType; null={Object}>"
|
||||
TokenLabelType() ::= "<file.TokenLabelType; null={Token}>"
|
||||
InputSymbolType() ::= "<file.InputSymbolType; null={Token}>"
|
||||
|
||||
TokenPropertyRef_text(t) ::= "(_localctx.<t.label>!=null?_localctx.<t.label>.getText():null)"
|
||||
TokenPropertyRef_type(t) ::= "(_localctx.<t.label>!=null?_localctx.<t.label>.getType():0)"
|
||||
|
@ -498,9 +499,9 @@ TokenPropertyRef_index(t) ::= "(_localctx.<t.label>!=null?_localctx.<t.label>.ge
|
|||
TokenPropertyRef_tree(t) ::= "_localctx.<t.label>_tree"
|
||||
TokenPropertyRef_int(t) ::= "(_localctx.<t.label>!=null?Integer.valueOf(_localctx.<t.label>.getText()):0)"
|
||||
|
||||
RulePropertyRef_start(r) ::= "(_localctx.<r.label>!=null?((<TokenLabelType()>)_localctx.<r.label>.start):null)"
|
||||
RulePropertyRef_stop(r) ::= "(_localctx.<r.label>!=null?((<TokenLabelType()>)_localctx.<r.label>.stop):null)"
|
||||
RulePropertyRef_tree(r) ::= "(_localctx.<r.label>!=null?((<ASTLabelType()>)_localctx.<r.label>.tree):null)"
|
||||
RulePropertyRef_start(r) ::= "(_localctx.<r.label>!=null?(_localctx.<r.label>.start):null)"
|
||||
RulePropertyRef_stop(r) ::= "(_localctx.<r.label>!=null?(_localctx.<r.label>.stop):null)"
|
||||
RulePropertyRef_tree(r) ::= "(_localctx.<r.label>!=null?(_localctx.<r.label>.tree):null)"
|
||||
RulePropertyRef_text(r) ::= "(_localctx.<r.label>!=null?_input.toString(_localctx.<r.label>.start,_localctx.<r.label>.stop):null)"
|
||||
RulePropertyRef_st(r) ::= "(_localctx.<r.label>!=null?_localctx.<r.label>.st:null)"
|
||||
|
||||
|
@ -550,7 +551,7 @@ ListLabelName(label) ::= "<label>_list"
|
|||
CaptureNextToken(d) ::= "<d.varName> = _input.LT(1);"
|
||||
CaptureNextTokenType(d) ::= "<d.varName> = _input.LA(1);"
|
||||
|
||||
StructDecl(s,attrs,visitorDispatchMethods,interfaces,extensionMembers,superClass={ParserRuleContext\<<TokenLabelType()>>}) ::= <<
|
||||
StructDecl(s,attrs,visitorDispatchMethods,interfaces,extensionMembers,superClass={ParserRuleContext\<<InputSymbolType()>>}) ::= <<
|
||||
public static class <s.name> extends <superClass><if(interfaces)> implements <interfaces; separator=", "><endif> {
|
||||
<attrs:{a | public <a>;}; separator="\n">
|
||||
<if(s.ctorAttrs)>public <s.name>(RuleContext parent, int state) { super(parent, state); }<endif>
|
||||
|
@ -571,7 +572,7 @@ public static class <s.name> extends <superClass><if(interfaces)> implements <in
|
|||
>>
|
||||
|
||||
TreeParserStructDecl(s,attrs,visitorDispatchMethods) ::= <<
|
||||
<StructDecl(superClass={TreeParserRuleContext\<<ASTLabelType()> >}, ...)>
|
||||
<StructDecl(superClass={ParserRuleContext\<<ASTLabelType()>\>}, ...)>
|
||||
>>
|
||||
|
||||
AltLabelStructDecl(s,attrs,visitorDispatchMethods) ::= <<
|
||||
|
@ -583,14 +584,14 @@ public static class <s.label>Context extends <currentRule.name>Context {
|
|||
|
||||
VisitorDispatchMethod(method) ::= <<
|
||||
@Override
|
||||
public void <if(method.isEnter)>enter<else>exit<endif>Rule(ParseTreeListener\<<TokenLabelType()>\> listener) {
|
||||
public void <if(method.isEnter)>enter<else>exit<endif>Rule(ParseTreeListener\<<InputSymbolType()>\> listener) {
|
||||
if ( listener!=null ) ((<parser.grammarName>Listener)listener).<if(method.isEnter)>enter<else>exit<endif>Rule(this);
|
||||
}
|
||||
>>
|
||||
|
||||
/*
|
||||
SwitchedVisitorDispatchMethod(method) ::= <<
|
||||
public void <if(method.isEnter)>enter<else>exit<endif>Rule(ParseTreeListener\<<TokenLabelType()>\> listener) {
|
||||
public void <if(method.isEnter)>enter<else>exit<endif>Rule(ParseTreeListener\<<InputSymbolType()>\> listener) {
|
||||
switch ( altNum ) {
|
||||
<method.listenerNames:{m |
|
||||
case <i> : ((<parser.grammarName>Listener)listener).<if(method.isEnter)>enter<else>exit<endif><m>(this); break;}; separator="\n">
|
||||
|
|
|
@ -36,6 +36,7 @@ public abstract class OutputFile extends OutputModelObject {
|
|||
public final String fileName;
|
||||
public final String TokenLabelType;
|
||||
public final String ASTLabelType;
|
||||
public final String InputSymbolType;
|
||||
|
||||
public OutputFile(OutputModelFactory factory, String fileName) {
|
||||
super(factory);
|
||||
|
@ -43,5 +44,7 @@ public abstract class OutputFile extends OutputModelObject {
|
|||
Grammar g = factory.getGrammar();
|
||||
TokenLabelType = g.getOptionString("TokenLabelType");
|
||||
ASTLabelType = g.getOptionString("ASTLabelType", "CommonAST");
|
||||
if ( g.isTreeGrammar() ) InputSymbolType = ASTLabelType;
|
||||
else InputSymbolType = TokenLabelType;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue