Package net.sf.lapg.api

Examples of net.sf.lapg.api.Grammar


      InputStream is = openInput(notifier);
      if (is == null) {
        return false;
      }

      Grammar s = SyntaxUtil.parseSyntax(options.getInput(), is, notifier, getDefaultOptions());
      if (s == null || s.hasErrors()) {
        return false;
      }

      Map<String, Object> genOptions = new HashMap<String, Object>(s.getOptions());
      Map<String, Object> additional = options.getAdditionalOptions();
      for (String key : additional.keySet()) {
        genOptions.put(key, additional.get(key));
      }

      long start = System.currentTimeMillis();
      ProcessingStatusAdapter adapter = new ProcessingStatusAdapter(notifier, options.getDebug());
      LexerTables l = LexicalBuilder.compile(s.getLexems(), adapter);
      ParserTables r = Builder.compile(s, adapter);
      if(l == null || r == null) {
        return false;
      }
      long generationTime = System.currentTimeMillis() - start;

      HashMap<String, Object> map = new HashMap<String, Object>();
      map.put("syntax", s);
      map.put("lex", l);
      map.put("parser", r);
      map.put("opts", genOptions);

      start = System.currentTimeMillis();
      generateOutput(map, s.getTemplates(), notifier);
      long textTime = System.currentTimeMillis() - start;
      notifier.info("lalr: " + generationTime/1000. + "s, text: " + textTime/1000. + "s\n");
      return true;
    } catch (Throwable t) {
      notifier.error("lapg: internal error: " + t.getClass().getName() + "\n");
View Full Code Here


  public static Grammar parseSyntax(String sourceName, InputStream stream, INotifier err,
      Map<String, Object> options) {
    String contents = getFileContents(stream);
    LapgTree<AstRoot> tree = LapgTree.parse(new TextSource(sourceName, contents.toCharArray(), 1));
    Grammar result = null;
    if (!tree.hasErrors()) {
      result = new LapgResolver(tree, options).resolve();
    }
    if (tree.hasErrors()) {
      result = null;
View Full Code Here

import net.sf.lapg.test.TestNotifier;

public class AnnotationsTest extends LapgTestCase {

  public void testAllAnnotations() {
    Grammar g = SyntaxUtil.parseSyntax("syntax1annotated", openStream("syntax1annotated", TESTCONTAINER),
        new TestNotifier(), new HashMap<String, Object>());
    Assert.assertNotNull(g);

    Rule[] listItemRules = rulesForName(g.getRules(), "list_item");
    Assert.assertEquals(2, listItemRules.length);

    Symbol s = listItemRules[0].getLeft();
    Assert.assertEquals(5, s.getAnnotation("weight"));
    Assert.assertEquals("wwo", s.getAnnotation("name"));

    Assert.assertEquals("rule1", listItemRules[0].getAnnotation("name"));
    Assert.assertEquals("rule2", listItemRules[1].getAnnotation("name"));

    Object val = listItemRules[0].getRight()[0].getAnnotation("ids");
    Assert.assertTrue(val instanceof List<?>);

    List<?> list = (List<?>) val;
    Assert.assertEquals(3, list.size());
    Assert.assertEquals(4, list.get(0));
    Assert.assertEquals(2, list.get(1));
    Assert.assertEquals(3, list.get(2));

    Rule[] inputRules = rulesForName(g.getRules(), "input");
    Assert.assertEquals(1, inputRules.length);
    Symbol input = inputRules[0].getLeft();

    Assert.assertNotNull(input);
    Object refval = input.getAnnotation("ref");
View Full Code Here

  }

  public void testBadAnnotations() {
    TestNotifier notifier = new TestNotifier("", "notexistingsym cannot be resolved\n"
        + "redeclaration of annotation `name' for non-terminal: tempanno, skipped\n");
    Grammar g = SyntaxUtil.parseSyntax("syntax1errannotated", openStream("syntax1errannotated", TESTCONTAINER),
        notifier, new HashMap<String, Object>());
    Assert.assertNull(g);
  }
View Full Code Here

    Assert.assertEquals(expected, actual);
  }

  public void testCheckSimple1() {
    Grammar g = SyntaxUtilOld.parseSyntax("syntax1", openStream("syntax1", TESTCONTAINER), new TestNotifier(),
        new HashMap<String, String>());
    Assert.assertNotNull(g);
    Assert.assertEquals(0, g.getEoi().getIndex());

    Symbol[] syms = g.getSymbols();
    Assert.assertEquals(7, syms.length);
    Assert.assertEquals("eoi", syms[0].getName());
    Assert.assertEquals("identifier", syms[1].getName());
    Assert.assertEquals("Licon", syms[2].getName());
    Assert.assertEquals("_skip", syms[3].getName()); // TODO do not
    // collect skip
    // symbols
    Assert.assertEquals("input", syms[4].getName());
    Assert.assertEquals("list", syms[5].getName());
    Assert.assertEquals("list_item", syms[6].getName());

    Rule[] rules = g.getRules();
    Assert.assertEquals(5, rules.length);
    Assert.assertEquals("input", rules[0].getLeft().getName());
    Assert.assertEquals("list", rules[0].getRight()[0].getTarget().getName());
    Assert.assertEquals(1, rules[0].getRight().length);

    Lexem[] lexems = g.getLexems();
    Assert.assertEquals(3, lexems.length);
    Assert.assertEquals("@?[a-zA-Z_][A-Za-z_0-9]*", lexems[0].getRegexp());
    Assert.assertEquals("([1-9][0-9]*|0[0-7]*|0[xX][0-9a-fA-F]+)([uU](l|L|ll|LL)?|(l|L|ll|LL)[uU]?)?", lexems[1]
        .getRegexp());
    Assert.assertEquals("[\\t\\r\\n ]+", lexems[2].getRegexp());
View Full Code Here

    checkGenTables(g, "syntax1.tbl", new TestNotifier());
  }

  public void testCheckSimple2() {
    Grammar g = SyntaxUtilOld.parseSyntax("syntax2", openStream("syntax2", TESTCONTAINER), new TestNotifier(),
        new HashMap<String, String>());
    Assert.assertNotNull(g);
    Assert.assertEquals(0, g.getEoi().getIndex());

    Symbol[] syms = g.getSymbols();
    Assert.assertEquals(9, syms.length);
    Assert.assertEquals("eoi", syms[0].getName());
    Assert.assertEquals("a", syms[1].getName());
    Assert.assertEquals("b", syms[2].getName());
    Assert.assertEquals("'('", syms[3].getName());
    Assert.assertEquals("')'", syms[4].getName());
    Assert.assertEquals("input", syms[5].getName());
    Assert.assertEquals("list", syms[6].getName());
    Assert.assertEquals("item", syms[7].getName());
    Assert.assertEquals("listopt", syms[8].getName());
    Assert.assertEquals(8, g.getRules().length);
    Assert.assertEquals("  ${for a in b}..!..$$  ", g.getRules()[7].getAction().getContents());

    checkGenTables(g, "syntax2.tbl", new TestNotifier());
  }
View Full Code Here

    checkGenTables(g, "syntax2.tbl", new TestNotifier());
  }

  public void testCheckCSharpGrammar() {
    Grammar g = SyntaxUtilOld.parseSyntax("syntax_cs", openStream("syntax_cs", TESTCONTAINER), new TestNotifier(),
        new HashMap<String, String>());
    Assert.assertNotNull(g);

    TestNotifier er = new TestNotifier(
        "syntax_cs,3: symbol `error` is useless\n" + "syntax_cs,44: symbol `Lfixed` is useless\n"
            + "syntax_cs,76: symbol `Lstackalloc` is useless\n"
            + "syntax_cs,149: symbol `comment` is useless\n" + "syntax_cs,155: symbol `'/*'` is useless\n"
            + "syntax_cs,157: symbol `anysym1` is useless\n" + "syntax_cs,159: symbol `'*/'` is useless\n",

        "input: using_directivesopt attributesopt modifiersopt Lclass ID class_baseopt '{' attributesopt modifiersopt operator_declarator '{' Lif '(' expression ')' embedded_statement\n"
            + "shift/reduce conflict (next: Lelse)\n"
            + "    embedded_statement ::= Lif '(' expression ')' embedded_statement\n"
            + "\n"
            + "conflicts: 1 shift/reduce and 0 reduce/reduce\n");

    checkGenTables(g, "syntax_cs.tbl", er);
    er.assertDone();

    Assert.assertTrue(g.getTemplates().startsWith("\n//#define DEBUG_syntax"));
  }
View Full Code Here

    Assert.assertTrue(g.getTemplates().startsWith("\n//#define DEBUG_syntax"));
  }

  public void testLapgGrammar() {
    Grammar g = SyntaxUtilOld.parseSyntax("syntax_lapg", openStream("syntax_lapg", TESTCONTAINER),
        new TestNotifier(), new HashMap<String, String>());
    Assert.assertNotNull(g);

    checkGenTables(g, "syntax_lapg.tbl", new TestNotifier());
  }
View Full Code Here

    checkGenTables(g, "syntax_lapg.tbl", new TestNotifier());
  }

  public void testLapgTemplatesGrammar() {
    Grammar g = SyntaxUtilOld.parseSyntax("syntax_tpl", openStream("syntax_tpl", TESTCONTAINER),
        new TestNotifier(), new HashMap<String, String>());
    Assert.assertNotNull(g);

    checkGenTables(g, "syntax_tpl.tbl", new TestNotifier());
  }
View Full Code Here

    checkGenTables(g, "syntax_tpl.tbl", new TestNotifier());
  }

  public void testNewTemplatesGrammar() {
    Grammar g = SyntaxUtil.parseSyntax("syntax_tpl", openStream("syntax_tpl", TESTCONTAINER), new TestNotifier(),
        new HashMap<String, Object>());
    Grammar go = SyntaxUtilOld.parseSyntax("syntax_tpl", openStream("syntax_tpl", TESTCONTAINER),
        new TestNotifier(), new HashMap<String, String>());
    Assert.assertNotNull(g);

    sortGrammar((LiGrammar) g, go);
    checkGenTables(g, "syntax_tpl.tbl", new TestNotifier());
View Full Code Here

TOP

Related Classes of net.sf.lapg.api.Grammar

Copyright © 2018 www.massapicom. 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.