コード例 #1
0
ファイル: Grammar.java プロジェクト: cyphermox/sablecc
    private void add(Context context) {

      if (!context.isNamed()) {
        throw new InternalException(
            "The anonymous context shouldn't be added to the global namespace");
      }

      internalAdd(context);
    }
コード例 #2
0
ファイル: Grammar.java プロジェクト: cyphermox/sablecc
  public void compileLexer(Trace trace, Strictness strictness) {

    Automaton automaton;
    if (this.globalAnonymousContext != null) {
      automaton = this.globalAnonymousContext.computeAutomaton().minimal().longest().minimal();
      automaton = checkAndApplyLexerPrecedence(automaton, trace, strictness).minimal();
      this.lexer.setAutomaton(automaton.withMarkers().minimal());
    } else {
      throw new InternalException("not implemented");
    }

    for (Context context : this.namedContexts) {
      context.computeAutomaton();
    }

    // Look for useless LexerExpression
    for (LexerExpression lexerExpression : this.lexer.getExpressions()) {
      // If their is no automaton saved it means that the LexerExpression
      // was not used to build the big automaton.
      if (lexerExpression.getSavedAutomaton() == null) {
        if (strictness == Strictness.STRICT) {
          throw SemanticException.genericError(
              "The " + lexerExpression.getExpressionName() + " expression is useless.");
        } else {
          trace.verboseln(
              "    The " + lexerExpression.getExpressionName() + " expression is useless.");
        }
      }
    }

    for (LexerExpression lexerExpression : this.lexer.getExpressions()) {
      // Note: getting the automaton forces the validation of the semantic
      // validity of (eg. cirularity)
      Automaton lexerAutomaton = lexerExpression.getAutomaton();
    }

    for (LexerExpression lexerExpression :
        this.globalAnonymousContext.getLexerExpressionTokensAndIgnored()) {
      // Note: The big automaton has to be minimal (thus with the unused
      // acceptations removed)
      if (!automaton.getAcceptations().contains(lexerExpression.getAcceptation())) {
        if (strictness == Strictness.STRICT) {
          throw SemanticException.genericError(
              "The " + lexerExpression.getExpressionName() + " token does not match anything.");
        } else {
          trace.verboseln(
              "    The " + lexerExpression.getExpressionName() + " token does not match anything.");
        }
      }

      Automaton expressionAutomaton = lexerExpression.getAutomaton();
      for (RichSymbol richSymbol : expressionAutomaton.getStartState().getTransitions().keySet()) {
        if (richSymbol.isLookahead()) {
          // We have a lookahead transition from the start state.
          // Note: this works since the result of getAutomaton() is
          // minimized.
          throw SemanticException.genericError(
              "The " + lexerExpression.getExpressionName() + " token matches the empty string.");
        }
      }
    }
  }