Java Code Examples for org.eclipse.xtext.xtext.generator.parser.antlr.AntlrGrammarGenUtil#toAntlrString()

The following examples show how to use org.eclipse.xtext.xtext.generator.parser.antlr.AntlrGrammarGenUtil#toAntlrString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XtendAntlrGrammarGeneratorHelper.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
public String compileTokens(final Grammar it, final AntlrOptions options) {
  StringConcatenation _builder = new StringConcatenation();
  _builder.newLine();
  _builder.append("tokens {");
  _builder.newLine();
  {
    LinkedHashSet<String> _tokens = this.getTokens(it);
    for(final String token : _tokens) {
      _builder.append("\t");
      _builder.append("KW_");
      String _javaIdentifier = this._grammarAccessExtensions.toJavaIdentifier(token, true);
      _builder.append(_javaIdentifier, "\t");
      _builder.append(" = \'");
      String _antlrString = AntlrGrammarGenUtil.toAntlrString(token);
      _builder.append(_antlrString, "\t");
      _builder.append("\' ;");
      _builder.newLineIfNotEmpty();
    }
  }
  _builder.append("}");
  _builder.newLine();
  return _builder.toString();
}
 
Example 2
Source File: UnicodeKeywordHelper.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Creates lexer rule for given keyword in order to handle JavaScript's unicode masks. E.g.:
 *
 * <pre>
 * If :
 * 	( 'i' | '\\' 'u' '0''0''6''9' )
 * 	( 'f' | '\\' 'u' '0''0''6''6' );
 * </pre>
 *
 * @param keyword
 *            the keyword as string, e.g. 'if'
 * @return the lexer body, e.g.
 *
 *         <pre>
 * ( 'i' | '\\' 'u' '0''0''6''9' ) ( 'f' | '\\' 'u' '0''0''6''6' )
 *         </pre>
 */
public static String toUnicodeKeyword(String keyword) {
	if (keyword.equals("async ")) {
		keyword = "async";
	}
	if (isIdentifier(keyword)) {
		StringBuilder result = new StringBuilder(keyword.length() * 30);
		for (char c : keyword.toCharArray()) {
			result.append("\n\t( '");
			result.append(c);
			result.append("' | '\\\\' 'u' ");
			String unicodeEscape = Strings.padStart(Integer.toHexString(c), 4, '0');
			for (char u : unicodeEscape.toCharArray()) {
				if ('0' <= u && u <= '9') {
					result.append("'");
					result.append(u);
					result.append("'");
				} else {
					result.append("( '");
					result.append(u);
					result.append("' | '");
					result.append(Character.toUpperCase(u));
					result.append("' )");
				}
			}
			result.append(" )");
		}
		return result.toString();
	}
	return "'" + AntlrGrammarGenUtil.toAntlrString(keyword) + "'";
}
 
Example 3
Source File: JFlexGeneratorFragmentTemplate.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public CharSequence state(final String it) {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("<YYINITIAL> \"");
  String _antlrString = AntlrGrammarGenUtil.toAntlrString(it);
  _builder.append(_antlrString);
  _builder.append("\" { return KW_");
  String _javaIdentifier = this._grammarAccessExtensions.toJavaIdentifier(it, true);
  _builder.append(_javaIdentifier);
  _builder.append("; }");
  return _builder;
}
 
Example 4
Source File: AbstractAntlrGrammarGenerator.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected String toAntlrKeywordRule(final String keyword, final AntlrOptions options) {
  boolean _isIgnoreCase = options.isIgnoreCase();
  if (_isIgnoreCase) {
    return AntlrGrammarGenUtil.toAntlrStringIgnoreCase(keyword);
  } else {
    String _antlrString = AntlrGrammarGenUtil.toAntlrString(keyword);
    String _plus = ("\'" + _antlrString);
    return (_plus + "\'");
  }
}
 
Example 5
Source File: AbstractAntlrGrammarGenerator.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected String ebnf(final Keyword it) {
  String _xifexpression = null;
  boolean _isCombinedGrammar = this.isCombinedGrammar();
  if (_isCombinedGrammar) {
    String _antlrString = AntlrGrammarGenUtil.toAntlrString(it.getValue());
    String _plus = ("\'" + _antlrString);
    _xifexpression = (_plus + "\'");
  } else {
    _xifexpression = this.keywordHelper.getRuleName(it.getValue());
  }
  return _xifexpression;
}
 
Example 6
Source File: AbstractAntlrGrammarGenerator.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected String _ebnf2(final EnumLiteralDeclaration it, final AntlrOptions options, final boolean supportActions) {
  String _xifexpression = null;
  boolean _isCombinedGrammar = this.isCombinedGrammar();
  if (_isCombinedGrammar) {
    String _antlrString = AntlrGrammarGenUtil.toAntlrString(it.getLiteral().getValue());
    String _plus = ("\'" + _antlrString);
    _xifexpression = (_plus + "\'");
  } else {
    _xifexpression = this.keywordHelper.getRuleName(it.getLiteral().getValue());
  }
  return _xifexpression;
}
 
Example 7
Source File: AbstractAntlrGeneratorFragment2.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected void cleanupParserTokensFile(final AntlrGrammar lexerGrammar, final AntlrGrammar parserGrammar, final KeywordHelper helper, final IXtextGeneratorFileSystemAccess fsa) {
  try {
    final MutableTokenDefProvider provider = this.createLexerTokensProvider(lexerGrammar, helper, fsa);
    Set<Map.Entry<Integer, String>> _entrySet = provider.getTokenDefMap().entrySet();
    for (final Map.Entry<Integer, String> entry : _entrySet) {
      {
        final String value = entry.getValue();
        boolean _isKeywordRule = helper.isKeywordRule(value);
        if (_isKeywordRule) {
          final String keywordAsAntlrString = AntlrGrammarGenUtil.toAntlrString(helper.getKeywordValue(value));
          entry.setValue((("\'" + keywordAsAntlrString) + "\'"));
        } else {
          boolean _startsWith = value.startsWith("\'");
          if (_startsWith) {
            String _antlrString = AntlrGrammarGenUtil.toAntlrString(value);
            String _plus = ("\'" + _antlrString);
            String _plus_1 = (_plus + "\'");
            entry.setValue(_plus_1);
          }
        }
      }
    }
    final CharArrayWriter writer = new CharArrayWriter();
    PrintWriter _printWriter = new PrintWriter(writer);
    provider.writeTokenFile(_printWriter);
    String _tokensFileName = parserGrammar.getTokensFileName();
    char[] _charArray = writer.toCharArray();
    String _string = new String(_charArray);
    fsa.generateFile(_tokensFileName, _string);
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}