Java org.antlr.v4.runtime.CharStream 代码实例

・12 分钟阅读

以下是展示如何使用org.antlr.v4.runtime.CharStream的最佳示例。 我们使用了代码质量辨别算法从开源项目中提取出了最佳的优秀示例。

实例 1


protected CharStream createInputStream(OffsetRegion span) throws BadLocationException {
    CharStream input;
    if (span.getLength() > 1000) {
        input = new DocumentSnapshotCharStream(snapshot, span);
    } else {
        input = new DocumentSnapshotCharStream(snapshot);
    }
    input.seek(span.getStart());
    return input;
}
 

实例 2


protected CharStream createInputStream(OffsetRegion span) throws BadLocationException {
    CharStream input;
    if (span.getLength() > 1000) {
        input = new DocumentCharStreamV4(this.document, span);
    } else {
        input = new DocumentCharStreamV4(this.document);
    }
    input.seek(span.getStart());
    return input;
}
 

实例 3


protected void accept(@NotNull CharStream input, LexerActionExecutor lexerActionExecutor,
                                          int startIndex, int index, int line, int charPos)
{
        if ( debug ) {
                System.out.format(Locale.getDefault(), "ACTION %sn", lexerActionExecutor);
        }
        // seek to after last char in token
        input.seek(index);
        this.line = line;
        this.charPositionInLine = charPos;
        if (input.LA(1) != IntStream.EOF) {
                consume(input);
        }
        if (lexerActionExecutor != null && recog != null) {
                lexerActionExecutor.execute(recog, input, startIndex);
        }
}
 

实例 4


/**
 * Evaluate a predicate specified in the lexer.
 *
 * <p>If {@code speculative} is {@code true}, this method was called before
 * {@link #consume} for the matched character. This method should call
 * {@link #consume} before evaluating the predicate to ensure position
 * sensitive values, including {@link Lexer#getText}, {@link Lexer#getLine},
 * and {@link Lexer#getCharPositionInLine}, properly reflect the current
 * lexer state. This method should restore {@code input} and the simulator
 * to the original state before returning (i.e. undo the actions made by the
 * call to {@link #consume}.</p>
 *
 * @param input The input stream.
 * @param ruleIndex The rule containing the predicate.
 * @param predIndex The index of the predicate within the rule.
 * @param speculative {@code true} if the current index in {@code input} is
 * one character before the predicate's location.
 *
 * @return {@code true} if the specified predicate evaluates to
 * {@code true}.
 */
protected boolean evaluatePredicate(@NotNull CharStream input, int ruleIndex, int predIndex, boolean speculative) {
        // assume true if no recognizer was provided
        if (recog == null) {
                return true;
        }
        if (!speculative) {
                return recog.sempred(null, ruleIndex, predIndex);
        }
        int savedCharPositionInLine = charPositionInLine;
        int savedLine = line;
        int index = input.index();
        int marker = input.mark();
        try {
                consume(input);
                return recog.sempred(null, ruleIndex, predIndex);
        }
        finally {
                charPositionInLine = savedCharPositionInLine;
                line = savedLine;
                input.seek(index);
                input.release(marker);
        }
}
 

实例 5


@Override
public int LA(int i) {
    if (i == 0) {
        // undefined
        return 0;
    }
    int currentIndex = index();
    int size = size();
    if (i < 0) {
        // e.g., translate LA(-1) to use offset i=0; then data[p+0-1]
        i++;
        if ((currentIndex + i - 1) < 0) {
            // invalid; no char before first char
            return CharStream.EOF;
        }
    }
    if ((currentIndex + i - 1) >= size) {
        return CharStream.EOF;
    }
    int actualIndex = currentIndex + i - 1;
    String currentLine = currentSnapshotLine;
    int currentLineStartIndex = currentSnapshotLineStartIndex;
    if (currentLine != null
        && actualIndex >= currentLineStartIndex
        && actualIndex < currentLineStartIndex + currentLine.length()) {
        return currentLine.charAt(actualIndex - currentLineStartIndex);
    }
    return getSnapshot().charAt(actualIndex);
}
 

实例 6


public int LA(int i) {
    if ( i==0 ) {
        return 0; // undefined
    }
    if ( i<0 ) {
        i++; // e.g., translate LA(-1) to use offset 0
    }
    if ( (p+i-1) >= n ) {
        return CharStream.EOF;
    }
    return Character.toLowerCase(data[p+i-1]);
}
 

实例 7


private int read() {
    int result = input.read();
    if (result == LexerInput.EOF) {
        result = CharStream.EOF;
    }
    return result;
}
 

实例 8


@Override
public int LA(int i) {
    if (i == 0) {
        // undefined
        return 0;
    }
    int currentIndex = index();
    int size = size();
    if (i < 0) {
        // e.g., translate LA(-1) to use offset i=0; then data[p+0-1]
        i++;
        if ((currentIndex + i - 1) < 0) {
            // invalid; no char before first char
            return CharStream.EOF;
        }
    }
    if ((currentIndex + i - 1) >= size) {
        return CharStream.EOF;
    }
    int actualIndex = currentIndex + i - 1;
    String currentLine = currentSnapshotLine;
    int currentLineStartIndex = currentSnapshotLineStartIndex;
    if (currentLine != null
        && actualIndex >= currentLineStartIndex
        && actualIndex < currentLineStartIndex + currentLine.length()) {
        return currentLine.charAt(actualIndex - currentLineStartIndex);
    }
    return getSnapshot().charAt(actualIndex);
}
 

实例 9


@Override
public int LA(int i) {
  int returnChar = super.LA(i);
  if (returnChar == CharStream.EOF) {
    return returnChar;
  } else if (returnChar == 0) {
    return returnChar;
  }
  return Character.toUpperCase((char) returnChar);
}
 

实例 10


/** Given a starting configuration set, figure out all ATN configurations
 *  we can reach upon input {@code t}. Parameter {@code reach} is a return
 *  parameter.
 */
protected void getReachableConfigSet(@NotNull CharStream input, @NotNull ATNConfigSet closure, @NotNull ATNConfigSet reach, int t) {
        // this is used to skip processing for configs which have a lower priority
        // than a config that already reached an accept state for the same rule
        int skipAlt = ATN.INVALID_ALT_NUMBER;
        for (ATNConfig c : closure) {
                boolean currentAltReachedAcceptState = c.alt == skipAlt;
                if (currentAltReachedAcceptState && ((LexerATNConfig)c).hasPassedThroughNonGreedyDecision()) {
                        continue;
                }
                if ( debug ) {
                        System.out.format(Locale.getDefault(), "testing %s at %sn", getTokenName(t), c.toString(recog, true));
                }
                int n = c.state.getNumberOfTransitions();
                for (int ti=0; ti<n; ti++) {               // for each transition
                        Transition trans = c.state.transition(ti);
                        ATNState target = getReachableTarget(trans, t);
                        if ( target!=null ) {
                                LexerActionExecutor lexerActionExecutor = ((LexerATNConfig)c).getLexerActionExecutor();
                                if (lexerActionExecutor != null) {
                                        lexerActionExecutor = lexerActionExecutor.fixOffsetBeforeMatch(input.index() - startIndex);
                                }
                                boolean treatEofAsEpsilon = t == CharStream.EOF;
                                if (closure(input, new LexerATNConfig((LexerATNConfig)c, target, lexerActionExecutor), reach, currentAltReachedAcceptState, true, treatEofAsEpsilon)) {
                                        // any remaining configs for this alt have a lower priority than
                                        // the one that just reached an accept state.
                                        skipAlt = c.alt;
                                        break;
                                }
                        }
                }
        }
}
 

实例 11


private int read() {
    int result = input.read();
    if (result == LexerInput.EOF) {
        result = CharStream.EOF;
    }
    return result;
}
 

实例 12


@Override
public int LA(int i)
{
    int result = stream.LA(i);
    switch (result) {
        case 0:
        case CharStream.EOF:
            return result;
        default:
            return Character.toUpperCase(result);
    }
}
 

实例 13


@Override
public int LA(int i) {
        if ( i==0 ) {
                return 0; // undefined
        }
        if ( i<0 ) {
                i++; // e.g., translate LA(-1) to use offset 0 
        }
        if ( (p+i-1) >= n ) {
           //System.out.println("char LA("+i+")=EOF; p="+p);
           return CharStream.EOF;
       }
       //System.out.println("char LA("+i+")="+data.charAt(p+i-1)+";p="+p);
        if (Character.isUpperCase(data[p+i-1]))
        return Character.toLowerCase(data[p+i-1]);
        return data[p+i-1];
}
 

实例 14


@Override
public int LA(int i) {
  int returnChar = super.LA(i);
  if (returnChar == CharStream.EOF) {
    return returnChar;
  } else if (returnChar == 0) {
    return returnChar;
  }
  return Character.toUpperCase((char) returnChar);
}
 

实例 15


public int LA(int i) {
if ( i==0 ) {
        return 0; // undefined
}
if ( i<0 ) {
        i++; // e.g., translate LA(-1) to use offset 0
}
if ( (p+i-1) >= n ) {
          return CharStream.EOF;
      }
      return Character.toLowerCase(data[p+i-1]);
  }
 
讨论
淘淘あ西西 profile image