All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.antlr.v4.codegen.target.JavaScriptTarget Maven / Gradle / Ivy

There is a newer version: 4.13.2
Show newest version
/*
 * Copyright (c) 2012-2016 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.tool.ast.GrammarAST;
import org.stringtemplate.v4.STGroup;
import org.stringtemplate.v4.StringRenderer;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;

/**
 *
 * @author Eric Vergnaud
 */
public class JavaScriptTarget extends Target {

	/** Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar */
	protected static final String[] javaScriptKeywords = {
		"break", "case", "class", "catch", "const", "continue", "debugger",
		"default", "delete", "do", "else", "export", "extends", "finally", "for",
		"function", "if", "import", "in", "instanceof", "let", "new", "return",
		"super", "switch", "this", "throw", "try", "typeof", "var", "void",
		"while", "with", "yield",
		//future reserved
		"enum", "await", "implements", "package", "protected", "static",
		"interface", "private", "public",
		//future reserved in older standards
		"abstract", "boolean", "byte", "char", "double", "final", "float",
		"goto", "int", "long", "native", "short", "synchronized", "transient",
		"volatile",
		//literals
		"null", "true", "false"
	};

	/** Avoid grammar symbols in this set to prevent conflicts in gen'd code. */
	protected final Set badWords = new HashSet();

	public JavaScriptTarget(CodeGenerator gen) {
		super(gen, "JavaScript");
	}

    @Override
    public String getVersion() {
        return "4.6";
    }

    public Set getBadWords() {
		if (badWords.isEmpty()) {
			addBadWords();
		}

		return badWords;
	}

	protected void addBadWords() {
		badWords.addAll(Arrays.asList(javaScriptKeywords));
		badWords.add("rule");
		badWords.add("parserRule");
	}

	/**
	 * {@inheritDoc}
	 * 

* For Java, this is the translation {@code 'a\n"'} → {@code "a\n\""}. * Expect single quotes around the incoming literal. Just flip the quotes * and replace double quotes with {@code \"}. *

* Note that we have decided to allow people to use '\"' without penalty, so * we must build the target string in a loop as {@link String#replace} * cannot handle both {@code \"} and {@code "} without a lot of messing * around. */ @Override public String getTargetStringLiteralFromANTLRStringLiteral( CodeGenerator generator, String literal, boolean addQuotes) { StringBuilder sb = new StringBuilder(); String is = literal; if ( addQuotes ) sb.append('"'); for (int i = 1; i < is.length() -1; i++) { if (is.charAt(i) == '\\') { // Anything escaped is what it is! We assume that // people know how to escape characters correctly. However // we catch anything that does not need an escape in Java (which // is what the default implementation is dealing with and remove // the escape. The C target does this for instance. // switch (is.charAt(i+1)) { // Pass through any escapes that Java also needs // case '"': case 'n': case 'r': case 't': case 'b': case 'f': case '\\': // Pass the escape through sb.append('\\'); break; case 'u': // Assume unnnn // Pass the escape through as double \\ // so that Java leaves as \u0000 string not char sb.append('\\'); sb.append('\\'); break; default: // Remove the escape by virtue of not adding it here // Thus \' becomes ' and so on break; } // Go past the \ character i++; } else { // Characters that don't need \ in ANTLR 'strings' but do in Java if (is.charAt(i) == '"') { // We need to escape " in Java sb.append('\\'); } } // Add in the next character, which may have been escaped sb.append(is.charAt(i)); } if ( addQuotes ) sb.append('"'); return sb.toString(); } @Override public String encodeIntAsCharEscape(int v) { if (v < Character.MIN_VALUE || v > Character.MAX_VALUE) { throw new IllegalArgumentException(String.format("Cannot encode the specified value: %d", v)); } if (v >= 0 && v < targetCharValueEscape.length && targetCharValueEscape[v] != null) { return targetCharValueEscape[v]; } if (v >= 0x20 && v < 127) { return String.valueOf((char)v); } String hex = Integer.toHexString(v|0x10000).substring(1,5); return "\\u"+hex; } @Override public int getSerializedATNSegmentLimit() { return 2 ^ 31; } @Override public int getInlineTestSetWordSize() { return 32; } @Override protected boolean visibleGrammarSymbolCausesIssueInGeneratedCode(GrammarAST idNode) { return getBadWords().contains(idNode.getText()); } @Override protected STGroup loadTemplates() { STGroup result = super.loadTemplates(); result.registerRenderer(String.class, new JavaStringRenderer(), true); return result; } 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); } } @Override public boolean wantsBaseListener() { return false; } @Override public boolean wantsBaseVisitor() { return false; } @Override public boolean supportsOverloadedMethods() { return false; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy