Example usage for org.antlr.v4.runtime CommonToken setText

List of usage examples for org.antlr.v4.runtime CommonToken setText

Introduction

In this page you can find the example usage for org.antlr.v4.runtime CommonToken setText.

Prototype

@Override
public void setText(String text) 

Source Link

Document

Explicitly set the text for this token.

Usage

From source file:PhpBaseLexer.java

License:Open Source License

@Override
public Token nextToken() {
    CommonToken token = (CommonToken) super.nextToken();

    if (token.getType() == PhpLexer.PHPEnd || token.getType() == PhpLexer.PHPEndSingleLineComment) {
        if (_mode == PhpLexer.SingleLineCommentMode) {
            // SingleLineCommentMode for such allowed syntax:
            // <?php echo "Hello world"; // comment ?>
            popMode(); // exit from SingleLineComment mode.
        }//  w ww.ja v a 2s .c o m
        popMode(); // exit from PHP mode.

        if (token.getText().equals("</script>")) {
            _phpScript = false;
            token.setType(PhpLexer.ScriptClose);
        } else {
            // Add semicolon to the end of statement if it is absente.
            // For example: <?php echo "Hello world" ?>
            if (_prevTokenType == PhpLexer.SemiColon || _prevTokenType == PhpLexer.Colon
                    || _prevTokenType == PhpLexer.OpenCurlyBracket
                    || _prevTokenType == PhpLexer.CloseCurlyBracket) {
                token.setChannel(PhpLexer.SkipChannel);
            } else {
                token = new CommonToken(PhpLexer.SemiColon);
            }
        }
    } else if (token.getType() == PhpLexer.HtmlName) {
        _htmlNameText = token.getText();
    } else if (token.getType() == PhpLexer.HtmlDoubleQuoteString) {
        if (token.getText().equals("php") && _htmlNameText.equals("language")) {
            _phpScript = true;
        }
    } else if (_mode == PhpLexer.HereDoc) {
        // Heredoc and Nowdoc syntax support: http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
        switch (token.getType()) {
        case PhpLexer.StartHereDoc:
        case PhpLexer.StartNowDoc:
            _heredocIdentifier = token.getText().substring(3).trim().replace("\'", "");
            break;

        case PhpLexer.HereDocText:
            if (CheckHeredocEnd(token.getText())) {
                popMode();

                String heredocIdentifier = GetHeredocIdentifier(token.getText());
                if (token.getText().trim().endsWith(";")) {
                    token = new CommonToken(PhpLexer.SemiColon, heredocIdentifier + ";\n");
                } else {
                    token = (CommonToken) super.nextToken();
                    token.setText(heredocIdentifier + "\n;");
                }
            }
            break;
        }
    } else if (_mode == PhpLexer.PHP) {
        if (_channel != PhpLexer.HIDDEN) {
            _prevTokenType = token.getType();
        }
    }

    return token;
}

From source file:controle.dicionario.Dicionario.java

private void analisaAssunto(String nomeArquivo, String texto) throws Exception {
    //ArrayList<String> tags = null;
    ArrayList<String> tagsMorf = null;
    CommonTokenStream tokensMorf = null;
    CommonTokenStream tokens = null;/* ww w.java 2 s .co  m*/
    Referencia referenciaBanco = referenciasHash.get(nomeArquivo);
    referenciaBanco.setQtd(0);
    referenciaDAO.atualiza(referenciaBanco);
    this.nomeArquivo = nomeArquivo;
    SrsGrammarLexer lexer = new SrsGrammarLexer(new ANTLRInputStream(texto));
    tokens = new CommonTokenStream(lexer);
    SrsGrammarParser parser = new SrsGrammarParser(tokens);
    System.out.println(tokens.getText());

    analisePeriodo = new AnalisePeriodo(texto);

    SrsGrammarLexer lexerMorf = new SrsGrammarLexer(new ANTLRInputStream(texto));
    tokensMorf = new CommonTokenStream(lexerMorf);
    System.out.println(tokensMorf.getText());
    tagsMorf = etiquetar(tokensMorf);

    try {
        Sigla sigla = new Sigla();
        siglas.putAll(sigla.reconheceSiglas(tokens));

    } catch (Exception e) {
        e.printStackTrace();
    }

    Iterator it1 = tokens.getTokens().iterator();
    CommonToken ob = null;
    int qtdPalavrasRelevantes = 0;
    int qtdRadicais = 0;

    while (it1.hasNext()) {
        ob = (CommonToken) it1.next();
        //Retirando o caractere '-' do inicio da palavra
        if (ob.getText().length() > 1 && ob.getText().charAt(0) == '-') {
            ob.setText(ob.getText().substring(1));
        }
        //Retirando o caractere '-' do fim da palavra
        if (ob.getText().length() > 1 && (ob.getText().charAt(ob.getText().length() - 1) == '-')) {
            ob.setText(ob.getText().substring(0, ob.getText().length() - 2));
        }

        String palavra = ob.getText().toLowerCase();
        String palavraLemmaa = ob.getText();

        //Filtrando as palavras relevantes do texto
        if ((ob.getType() == SrsGrammarLexer.PALAVRA) && (ob.getText().length() > 4)
                && stopWordsHash.get(palavra) == null && !ehTudoIgual(palavra)) {
            qtdPalavrasRelevantes++;

            String radical;
            //Recuperando radical da palavra
            //Obtem radical utilizando o PTStemmer
            radical = getRadicalPTStemmer(palavra);

            String lemma;
            //Obtem lemma utilizando o TreeTagger
            //                lemma = AnalisePeriodo.tagger.getLemma(palavraLemmaa);
            lemma = AnalisePeriodo.recuperaLemma(palavraLemmaa);
            palavraLemma.put(palavraLemmaa, lemma);

            Lemma lemmaBanco = new Lemma(null, lemma);
            Radical radicalBanco = new Radical(null, radical);
            Palavra palavraBanco = new Palavra(null, palavra, lemmaBanco, radicalBanco);

            if (!palavrasHash.containsKey(palavra)) {
                palavrasHash.put(palavra, palavraBanco);
            }

            if (!radicaisHash.containsKey(radical)) {
                radicaisHash.put(radical, radicalBanco);
            }

            if (!lemmaHash.containsKey(lemma)) {
                lemmaHash.put(lemma, lemmaBanco);
            }

            Dados dadosBanco = new Dados();
            dadosBanco.setF(1);
            dadosBanco.setIdPalavra(palavraBanco);
            dadosBanco.setIdRadical(radicalBanco);
            dadosBanco.setIdReferencia(referenciaBanco);
            dadosBanco.setIdProjeto(projetoSelecionado);
            String chaveDados = projetoSelecionado.getId() + referenciaBanco.getId() + radical + palavra;
            if (!dadosHash.containsKey(chaveDados)) {
                dadosHash.put(chaveDados, dadosBanco);
                palavraBanco = palavrasHash.get(palavra);
                dadosBanco.setQtdDoc(dadosBanco.getQtdDoc() + 1);
            } else {
                Integer i = dadosHash.get(chaveDados).getF() + 1;
                dadosBanco.setF(i);
                dadosBanco.setQtdDoc(1);
                dadosHash.replace(chaveDados, dadosBanco);
            }

            DadoRadical dadoRadicalBanco = new DadoRadical();
            dadoRadicalBanco.setF(1);
            dadoRadicalBanco.setIdRadical(radicalBanco);
            dadoRadicalBanco.setIdReferencia(referenciaBanco);
            dadoRadicalBanco.setIdProjeto(projetoSelecionado);
            String chaveDadoRadical = projetoSelecionado.getId() + referenciaBanco.getId() + radical;
            if (!dadoRadicalHash.containsKey(chaveDadoRadical)) {
                dadoRadicalHash.put(chaveDadoRadical, dadoRadicalBanco);
                radicalBanco = radicaisHash.get(radical);
                dadoRadicalBanco.setQtdDoc(dadoRadicalBanco.getQtdDoc() + 1);
            } else {
                Integer i = dadoRadicalHash.get(chaveDadoRadical).getF() + 1;
                dadoRadicalBanco.setF(i);
                dadoRadicalBanco.setQtdDoc(1);
                dadoRadicalHash.replace(chaveDadoRadical, dadoRadicalBanco);
            }

        }
    }
    referenciaBanco.setQtdRelevante(qtdPalavrasRelevantes);
    referenciaBanco.setPercRelevante(((float) qtdPalavrasRelevantes / tokens.size()) * 100);
    referenciaDAO.atualiza(referenciaBanco);

    tagsMorf = etiquetar(tokensMorf);

    ParseTree tree;
    tree = parser.init();
    oracoes = parser.oracoes;
    ArrayList<String> oracoes1 = new ArrayList<>();
    for (String oracao : oracoes) {
        if (!oracao.equals("\n")) {
            oracoes1.add(oracao);
        }
    }
    parser.oracoes.clear();
    parser.oracoes.addAll(oracoes1);

    addAbreviatura();

}

From source file:edu.clemson.resolve.misc.Utils.java

License:BSD License

/**
 * Returns a new {@link CommonToken} from some arbtrary existing {@code Token}. This is useful for when you want
 * create a {@code Token} consisting of {@code desiredText}, but using existing location information from {@code t}.
 * <p>/*  w ww .j  a  v a2s  .  c  o  m*/
 * <strong>NOTE:</strong> if {@code desiredText} is {@code null}, then the text for the resulting {@code Token}
 * will contain whatever text was already in {@code t} starting out.</p>
 *
 * @param t           an existing token (preferably near where {@code desiredText} should appear)
 * @param desiredText the text we want the resulting token to hold
 *
 * @return a new token
 */
@NotNull
public static CommonToken createTokenFrom(@NotNull Token t, @Nullable String desiredText) {
    CommonToken result = new CommonToken(t);
    if (desiredText != null) {
        result.setText(desiredText);
    }
    return result;
}

From source file:org.eclipse.titan.designer.parsers.ttcn3parser.PPDirectiveTokenFactory.java

License:Open Source License

@Override
public CommonToken create(Pair<TokenSource, CharStream> source, int type, String text, int channel, int start,
        int stop, int line, int charPositionInLine) {
    CommonToken t = new CommonToken(source, type, channel, start, stop);
    t.setLine(line);/*from  w ww.j  a  v  a 2s  . c o  m*/
    t.setStartIndex(start + token.getStartIndex());
    t.setStopIndex(stop + token.getStartIndex() + 1);
    t.setCharPositionInLine(charPositionInLine);
    if (text != null) {
        t.setText(text);
    } else if (copyText && source.b != null) {
        t.setText(source.b.getText(Interval.of(start, stop)));
    }

    return t;
}