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

org.xtext.vim.generator.TerminalRuleToRegEx Maven / Gradle / Ivy

The newest version!
package org.xtext.vim.generator;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.AbstractElement;
import org.eclipse.xtext.AbstractRule;
import org.eclipse.xtext.Alternatives;
import org.eclipse.xtext.CharacterRange;
import org.eclipse.xtext.EOF;
import org.eclipse.xtext.Group;
import org.eclipse.xtext.Keyword;
import org.eclipse.xtext.NegatedToken;
import org.eclipse.xtext.ParserRule;
import org.eclipse.xtext.RuleCall;
import org.eclipse.xtext.TerminalRule;
import org.eclipse.xtext.UntilToken;
import org.eclipse.xtext.Wildcard;
import org.eclipse.xtext.util.Strings;
import org.eclipse.xtext.util.XtextSwitch;
import org.eclipse.xtext.xtext.generator.parser.antlr.AntlrGrammarGenUtil;

/**
 * Comment
 * Constant
 * Identifier
 * Statement
 * Operator
 * PreProc
 * Type
 * Special
 * Underlined
 * Ignore
 * Error
 * Todo
 */
@SuppressWarnings("all")
public class TerminalRuleToRegEx extends XtextSwitch {
  private final StringBuilder result;
  
  private TerminalRuleToRegEx() {
    StringBuilder _stringBuilder = new StringBuilder();
    this.result = _stringBuilder;
  }
  
  public static String toRegEx(final TerminalRule rule) {
    TerminalRuleToRegEx _terminalRuleToRegEx = new TerminalRuleToRegEx();
    return _terminalRuleToRegEx.print(rule);
  }
  
  public String print(final TerminalRule rule) {
    AbstractElement _alternatives = rule.getAlternatives();
    this.doSwitch(_alternatives);
    return this.result.toString();
  }
  
  private static String veryMagicEscape(final String str) {
    String _replaceAll = str.replaceAll("\\n", "\\\\n");
    String _replaceAll_1 = _replaceAll.replaceAll("\\r\\n", "\\\\r\\\\n");
    String _replaceAll_2 = _replaceAll_1.replaceAll(".", "\\.");
    String _replaceAll_3 = _replaceAll_2.replaceAll("\\{", "\\\\{");
    return _replaceAll_3.replaceAll("\\\\", "\\\\\\\\");
  }
  
  @Override
  public String caseAlternatives(final Alternatives object) {
    Character _valueOf = Character.valueOf('(');
    char _charValue = _valueOf.charValue();
    this.result.append(_charValue);
    boolean first = true;
    EList _elements = object.getElements();
    for (final AbstractElement elem : _elements) {
      {
        if ((!first)) {
          Character _valueOf_1 = Character.valueOf('|');
          char _charValue_1 = _valueOf_1.charValue();
          this.result.append(_charValue_1);
        }
        first = false;
        this.doSwitch(elem);
      }
    }
    Character _valueOf_1 = Character.valueOf(')');
    char _charValue_1 = _valueOf_1.charValue();
    StringBuilder _append = this.result.append(_charValue_1);
    String _cardinality = object.getCardinality();
    String _emptyIfNull = Strings.emptyIfNull(_cardinality);
    _append.append(_emptyIfNull);
    return "";
  }
  
  @Override
  public String caseCharacterRange(final CharacterRange object) {
    String _cardinality = object.getCardinality();
    boolean _isEmpty = Strings.isEmpty(_cardinality);
    boolean _not = (!_isEmpty);
    if (_not) {
      Character _valueOf = Character.valueOf('(');
      char _charValue = _valueOf.charValue();
      this.result.append(_charValue);
    }
    Keyword _left = object.getLeft();
    this.doSwitch(_left);
    this.result.append("..");
    Keyword _right = object.getRight();
    this.doSwitch(_right);
    String _cardinality_1 = object.getCardinality();
    boolean _isEmpty_1 = Strings.isEmpty(_cardinality_1);
    boolean _not_1 = (!_isEmpty_1);
    if (_not_1) {
      Character _valueOf_1 = Character.valueOf(')');
      char _charValue_1 = _valueOf_1.charValue();
      this.result.append(_charValue_1);
      String _cardinality_2 = object.getCardinality();
      String _emptyIfNull = Strings.emptyIfNull(_cardinality_2);
      this.result.append(_emptyIfNull);
    }
    return "";
  }
  
  @Override
  public String defaultCase(final EObject object) {
    StringConcatenation _builder = new StringConcatenation();
    EClass _eClass = object.eClass();
    String _name = _eClass.getName();
    _builder.append(_name, "");
    _builder.append(" is not a valid argument.");
    throw new IllegalArgumentException(_builder.toString());
  }
  
  @Override
  public String caseGroup(final Group object) {
    String _cardinality = object.getCardinality();
    boolean _isEmpty = Strings.isEmpty(_cardinality);
    boolean _not = (!_isEmpty);
    if (_not) {
      Character _valueOf = Character.valueOf('(');
      char _charValue = _valueOf.charValue();
      this.result.append(_charValue);
    }
    boolean first = true;
    EList _elements = object.getElements();
    for (final AbstractElement elem : _elements) {
      {
        if ((!first)) {
          Character _valueOf_1 = Character.valueOf('|');
          char _charValue_1 = _valueOf_1.charValue();
          this.result.append(_charValue_1);
        }
        first = false;
        this.doSwitch(elem);
      }
    }
    String _cardinality_1 = object.getCardinality();
    boolean _isEmpty_1 = Strings.isEmpty(_cardinality_1);
    boolean _not_1 = (!_isEmpty_1);
    if (_not_1) {
      Character _valueOf_1 = Character.valueOf(')');
      char _charValue_1 = _valueOf_1.charValue();
      this.result.append(_charValue_1);
    }
    String _cardinality_2 = object.getCardinality();
    String _emptyIfNull = Strings.emptyIfNull(_cardinality_2);
    this.result.append(_emptyIfNull);
    return "";
  }
  
  @Override
  public String caseKeyword(final Keyword object) {
    String _value = object.getValue();
    String value = TerminalRuleToRegEx.veryMagicEscape(_value);
    this.result.append(value);
    String _cardinality = object.getCardinality();
    String _emptyIfNull = Strings.emptyIfNull(_cardinality);
    this.result.append(_emptyIfNull);
    return "";
  }
  
  @Override
  public String caseWildcard(final Wildcard object) {
    Character _valueOf = Character.valueOf('.');
    char _charValue = _valueOf.charValue();
    this.result.append(_charValue);
    String _cardinality = object.getCardinality();
    String _emptyIfNull = Strings.emptyIfNull(_cardinality);
    this.result.append(_emptyIfNull);
    return "";
  }
  
  @Override
  public String caseEOF(final EOF object) {
    this.result.append("EOF");
    String _cardinality = object.getCardinality();
    String _emptyIfNull = Strings.emptyIfNull(_cardinality);
    this.result.append(_emptyIfNull);
    return "";
  }
  
  @Override
  public String caseTerminalRule(final TerminalRule object) {
    String _ruleName = AntlrGrammarGenUtil.getRuleName(object);
    this.result.append(_ruleName);
    return "";
  }
  
  @Override
  public String caseParserRule(final ParserRule object) {
    throw new IllegalStateException("Cannot call parser rules that are not terminal rules.");
  }
  
  @Override
  public String caseRuleCall(final RuleCall object) {
    AbstractRule _rule = object.getRule();
    this.doSwitch(_rule);
    String _cardinality = object.getCardinality();
    String _emptyIfNull = Strings.emptyIfNull(_cardinality);
    this.result.append(_emptyIfNull);
    return "";
  }
  
  @Override
  public String caseNegatedToken(final NegatedToken object) {
    this.result.append("[^");
    AbstractElement _terminal = object.getTerminal();
    this.doSwitch(_terminal);
    StringBuilder _append = this.result.append("]");
    String _cardinality = object.getCardinality();
    String _emptyIfNull = Strings.emptyIfNull(_cardinality);
    _append.append(_emptyIfNull);
    return "";
  }
  
  @Override
  public String caseUntilToken(final UntilToken object) {
    this.result.append(".\\{-}");
    AbstractElement _terminal = object.getTerminal();
    this.doSwitch(_terminal);
    return "";
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy