Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (101)
JavaBeans (21)
JDBC (121)
JDK (426)
JSP (20)
Logging (108)
Mail (58)
Messaging (8)
Network (84)
PDF (97)
Report (7)
Scripting (84)
Security (32)
Server (121)
Servlet (26)
SOAP (24)
Testing (54)
Web (15)
XML (309)
Collections:
Other Resources:
ANTLR Tool Source Code
ANTLR is a powerful parser generator for multiple programming languages including Java.
ANTLR contains 2 major modules:
ANTLR Tool Source Code files are provided in the distribution packge (antlr4-4.10.1.zip). You can download them at ANTLR Website.
You can also browse the source code below:
✍: FYIcenter
⏎ org/antlr/v4/codegen/target/GoTarget.java
/* * Copyright (c) 2012-2017 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. */ package org.antlr.v4.codegen.target; import org.antlr.v4.codegen.CodeGenerator; import org.antlr.v4.codegen.Target; import org.antlr.v4.parse.ANTLRParser; import org.antlr.v4.tool.Grammar; import org.stringtemplate.v4.ST; import org.stringtemplate.v4.STGroup; import org.stringtemplate.v4.StringRenderer; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.*; public class GoTarget extends Target { protected static final HashSet<String> reservedWords = new HashSet<>(Arrays.asList( // keywords "break", "default", "func", "interface", "select", "case", "defer", "go", "map", "struct", "chan", "else", "goto", "package", "switch", "const", "fallthrough", "if", "range", "type", "continue", "for", "import", "return", "var", // predeclared identifiers https://golang.org/ref/spec#Predeclared_identifiers "bool", "byte", "complex64", "complex128", "error", "float32", "float64", "int", "int8", "int16", "int32", "int64", "rune", "string", "uint", "uint8", "uint16", "uint32", "uint64", "uintptr", "true", "false", "iota", "nil", "append", "cap", "close", "complex", "copy", "delete", "imag", "len", "make", "new", "panic", "print", "println", "real", "recover", // interface definition of RuleContext from runtime/Go/antlr/rule_context.go "Accept", "GetAltNumber", "GetBaseRuleContext", "GetChild", "GetChildCount", "GetChildren", "GetInvokingState", "GetParent", "GetPayload", "GetRuleContext", "GetRuleIndex", "GetSourceInterval", "GetText", "IsEmpty", "SetAltNumber", "SetInvokingState", "SetParent", "String", // misc "rule", "parserRule", "action" )); private static final boolean DO_GOFMT = !Boolean.parseBoolean(System.getenv("ANTLR_GO_DISABLE_GOFMT")) && !Boolean.parseBoolean(System.getProperty("antlr.go.disable-gofmt")); public GoTarget(CodeGenerator gen) { super(gen); } @Override protected Set<String> getReservedWords() { return reservedWords; } @Override protected void genFile(Grammar g, ST outputFileST, String fileName) { super.genFile(g, outputFileST, fileName); if (DO_GOFMT && !fileName.startsWith(".") /* criterion taken from gofmt */ && fileName.endsWith(".go")) { gofmt(new File(getCodeGenerator().tool.getOutputDirectory(g.fileName), fileName)); } } private void gofmt(File fileName) { // Optimistically run gofmt. If this fails, it doesn't matter at this point. Wait for termination though, // because "gofmt -w" uses ioutil.WriteFile internally, which means it literally writes in-place with O_TRUNC. // That could result in a race. (Why oh why doesn't it do tmpfile + rename?) try { // TODO: need something like: String goExecutable = locateGo(); ProcessBuilder gofmtBuilder = new ProcessBuilder("gofmt", "-w", "-s", fileName.getPath()); gofmtBuilder.redirectErrorStream(true); Process gofmt = gofmtBuilder.start(); InputStream stdout = gofmt.getInputStream(); // TODO(wjkohnen): simplify to `while (stdout.Read() > 1) {}` byte[] buf = new byte[1 << 10]; for (int l = 0; l > -1; l = stdout.read(buf)) { // There should not be any output that exceeds the implicit output buffer. In normal ops there should be // zero output. In case there is output, blocking and therefore killing the process is acceptable. This // drains the buffer anyway to play it safe. // dirty debug (change -w above to -d): // System.err.write(buf, 0, l); } gofmt.waitFor(); } catch (IOException e) { // Probably gofmt not in $PATH, in any case ignore. } catch (InterruptedException forward) { Thread.currentThread().interrupt(); } } @Override public int getInlineTestSetWordSize() { return 32; } @Override protected STGroup loadTemplates() { STGroup result = super.loadTemplates(); result.registerRenderer(String.class, new JavaStringRenderer(), true); return result; } public String getRecognizerFileName(boolean header) { CodeGenerator gen = getCodeGenerator(); Grammar g = gen.g; assert g!=null; String name; switch ( g.getType()) { case ANTLRParser.PARSER: name = g.name.endsWith("Parser") ? g.name.substring(0, g.name.length()-6) : g.name; return name.toLowerCase()+"_parser.go"; case ANTLRParser.LEXER: name = g.name.endsWith("Lexer") ? g.name.substring(0, g.name.length()-5) : g.name; // trim off "lexer" return name.toLowerCase()+"_lexer.go"; case ANTLRParser.COMBINED: return g.name.toLowerCase()+"_parser.go"; default : return "INVALID_FILE_NAME"; } } /** A given grammar T, return the listener name such as * TListener.java, if we're using the Java target. */ public String getListenerFileName(boolean header) { CodeGenerator gen = getCodeGenerator(); Grammar g = gen.g; assert g.name != null; return g.name.toLowerCase()+"_listener.go"; } /** A given grammar T, return the visitor name such as * TVisitor.java, if we're using the Java target. */ public String getVisitorFileName(boolean header) { CodeGenerator gen = getCodeGenerator(); Grammar g = gen.g; assert g.name != null; return g.name.toLowerCase()+"_visitor.go"; } /** A given grammar T, return a blank listener implementation * such as TBaseListener.java, if we're using the Java target. */ public String getBaseListenerFileName(boolean header) { CodeGenerator gen = getCodeGenerator(); Grammar g = gen.g; assert g.name != null; return g.name.toLowerCase()+"_base_listener.go"; } /** A given grammar T, return a blank listener implementation * such as TBaseListener.java, if we're using the Java target. */ public String getBaseVisitorFileName(boolean header) { CodeGenerator gen = getCodeGenerator(); Grammar g = gen.g; assert g.name != null; return g.name.toLowerCase()+"_base_visitor.go"; } protected static class JavaStringRenderer extends StringRenderer { @Override public String toString(Object o, String formatString, Locale locale) { if ("java-escape".equals(formatString)) { // 5C is the hex code for the \ itself return ((String)o).replace("\\u", "\\u005Cu"); } return super.toString(o, formatString, locale); } } }
⏎ org/antlr/v4/codegen/target/GoTarget.java
Or download all of them as a single archive file:
File name: antlr-tool-4.10.1-sources.jar File size: 347718 bytes Release date: 2022-04-15 Download
2022-04-24, 27686👍, 0💬
Popular Posts:
What Is jsse.jar (JDK 6) Java Secure Socket Extension? jsse.jar, Java Secure Socket Extension, is Ja...
What Is HttpComponents httpcore-4.2.2.jar? HttpComponents httpcore-4.2.2.jar is the JAR file for Apa...
io.jar is a component in iText Java library to provide input/output functionalities. iText Java libr...
What Is log4j-1.2.15.jar? I got the JAR file from apache-log4j-1.2.15.zip. log4j-1.2.15.jar is the v...
JDK 11 jdk.jshell.jmod is the JMOD file for JDK 11 JShell tool, which can be invoked by the "jshell"...