Package org.antlr.v4.test

Source Code of org.antlr.v4.test.TestATNDeserialization

/*
* [The "BSD license"]
*  Copyright (c) 2012 Terence Parr
*  Copyright (c) 2012 Sam Harwell
*  All rights reserved.
*
*  Redistribution and use in source and binary forms, with or without
*  modification, are permitted provided that the following conditions
*  are met:
*
*  1. Redistributions of source code must retain the above copyright
*     notice, this list of conditions and the following disclaimer.
*  2. Redistributions in binary form must reproduce the above copyright
*     notice, this list of conditions and the following disclaimer in the
*     documentation and/or other materials provided with the distribution.
*  3. The name of the author may not be used to endorse or promote products
*     derived from this software without specific prior written permission.
*
*  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
*  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
*  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
*  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
*  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
*  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
*  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
*  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
*  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package org.antlr.v4.test;

import org.antlr.v4.runtime.atn.ATN;
import org.antlr.v4.runtime.atn.ATNDeserializer;
import org.antlr.v4.runtime.atn.ATNSerializer;
import org.antlr.v4.runtime.misc.Utils;
import org.antlr.v4.tool.Grammar;
import org.antlr.v4.tool.LexerGrammar;
import org.junit.Test;

import static org.junit.Assert.*;

import java.util.Arrays;

public class TestATNDeserialization extends BaseTest {
  @Test public void testSimpleNoBlock() throws Exception {
    Grammar g = new Grammar(
      "parser grammar T;\n"+
      "a : A B ;");
    checkDeserializationIsStable(g);
  }

  @Test public void testEOF() throws Exception {
    Grammar g = new Grammar(
      "parser grammar T;\n"+
      "a : EOF ;");
    checkDeserializationIsStable(g);
  }

  @Test public void testEOFInSet() throws Exception {
    Grammar g = new Grammar(
      "parser grammar T;\n"+
      "a : (EOF|A) ;");
    checkDeserializationIsStable(g);
  }

  @Test public void testNot() throws Exception {
    Grammar g = new Grammar(
      "parser grammar T;\n"+
      "tokens {A, B, C}\n" +
      "a : ~A ;");
    checkDeserializationIsStable(g);
  }

  @Test public void testWildcard() throws Exception {
    Grammar g = new Grammar(
      "parser grammar T;\n"+
      "tokens {A, B, C}\n" +
      "a : . ;");
    checkDeserializationIsStable(g);
  }

  @Test public void testPEGAchillesHeel() throws Exception {
    Grammar g = new Grammar(
      "parser grammar T;\n"+
      "a : A | A B ;");
    checkDeserializationIsStable(g);
  }

  @Test public void test3Alts() throws Exception {
    Grammar g = new Grammar(
      "parser grammar T;\n"+
      "a : A | A B | A B C ;");
    checkDeserializationIsStable(g);
  }

  @Test public void testSimpleLoop() throws Exception {
    Grammar g = new Grammar(
      "parser grammar T;\n"+
      "a : A+ B ;");
    checkDeserializationIsStable(g);
  }

  @Test public void testRuleRef() throws Exception {
    Grammar g = new Grammar(
      "parser grammar T;\n"+
      "a : e ;\n" +
      "e : E ;\n");
    checkDeserializationIsStable(g);
  }

  @Test public void testLexerTwoRules() throws Exception {
    LexerGrammar lg = new LexerGrammar(
      "lexer grammar L;\n"+
      "A : 'a' ;\n" +
      "B : 'b' ;\n");
    checkDeserializationIsStable(lg);
  }

  @Test public void testLexerEOF() throws Exception {
    LexerGrammar lg = new LexerGrammar(
      "lexer grammar L;\n"+
      "A : 'a' EOF ;\n");
    checkDeserializationIsStable(lg);
  }

  @Test public void testLexerEOFInSet() throws Exception {
    LexerGrammar lg = new LexerGrammar(
      "lexer grammar L;\n"+
      "A : 'a' (EOF|'\\n') ;\n");
    checkDeserializationIsStable(lg);
  }

  @Test public void testLexerRange() throws Exception {
    LexerGrammar lg = new LexerGrammar(
      "lexer grammar L;\n"+
      "INT : '0'..'9' ;\n");
    checkDeserializationIsStable(lg);
  }

  @Test public void testLexerLoops() throws Exception {
    LexerGrammar lg = new LexerGrammar(
      "lexer grammar L;\n"+
      "INT : '0'..'9'+ ;\n");
    checkDeserializationIsStable(lg);
  }

  @Test public void testLexerNotSet() throws Exception {
    LexerGrammar lg = new LexerGrammar(
      "lexer grammar L;\n"+
      "ID : ~('a'|'b')\n ;");
    checkDeserializationIsStable(lg);
  }

  @Test public void testLexerNotSetWithRange() throws Exception {
    LexerGrammar lg = new LexerGrammar(
      "lexer grammar L;\n"+
      "ID : ~('a'|'b'|'e'|'p'..'t')\n ;");
    checkDeserializationIsStable(lg);
  }

  @Test public void testLexerNotSetWithRange2() throws Exception {
    LexerGrammar lg = new LexerGrammar(
      "lexer grammar L;\n"+
      "ID : ~('a'|'b') ~('e'|'p'..'t')\n ;");
    checkDeserializationIsStable(lg);
  }

  @Test public void test2ModesInLexer() throws Exception {
    LexerGrammar lg = new LexerGrammar(
      "lexer grammar L;\n"+
      "A : 'a'\n ;\n" +
      "mode M;\n" +
      "B : 'b';\n" +
      "mode M2;\n" +
      "C : 'c';\n");
    checkDeserializationIsStable(lg);
  }

  protected void checkDeserializationIsStable(Grammar g) {
    ATN atn = createATN(g, false);
    char[] data = Utils.toCharArray(ATNSerializer.getSerialized(atn));
    String atnData = ATNSerializer.getDecoded(atn, Arrays.asList(g.getTokenNames()));
    ATN atn2 = new ATNDeserializer().deserialize(data);
    String atn2Data = ATNSerializer.getDecoded(atn2, Arrays.asList(g.getTokenNames()));

    assertEquals(atnData, atn2Data);
  }
}
TOP

Related Classes of org.antlr.v4.test.TestATNDeserialization

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.