Beispiel #1
0
 @Test
 public void testQuoting() {
   assertTrue(Parser.needsQuoting("a "));
   assertTrue(Parser.needsQuoting("abraca,dabra"));
   assertTrue(Parser.needsQuoting("abraca}}dabra"));
   assertTrue(!Parser.needsQuoting("abracadabra"));
 }
Beispiel #2
0
  public int run(String... args) {
    Parser parser;
    Command command;

    parser = Parser.create(schema, getClass());
    try {
      command = (Command) parser.run(this, args);
      console.verbose.println("command line: " + Arrays.asList(args));
      if (pretend) {
        console.info.println("pretend-only, command " + command + " is not executed");
      } else {
        command.invoke();
      }
    } catch (ArgumentException e) {
      console.error.println(e.getMessage());
      console.info.println("Specify 'help' to get a usage message.");
      e.printStackTrace(exception ? console.error : console.verbose);
      return -1;
    } catch (RuntimeException e) {
      console.error.println(e.getMessage());
      e.printStackTrace(console.error);
      return -1;
    } catch (Exception e) {
      console.error.println(e.getMessage());
      e.printStackTrace(exception ? console.error : console.verbose);
      return -1;
    }
    return 0;
  }
  public void setImportPrecedence(final int precedence) {
    // Set import precedence for this stylesheet
    _importPrecedence = precedence;

    // Set import precedence for all included stylesheets
    final Enumeration elements = elements();
    while (elements.hasMoreElements()) {
      SyntaxTreeNode child = (SyntaxTreeNode) elements.nextElement();
      if (child instanceof Include) {
        Stylesheet included = ((Include) child).getIncludedStylesheet();
        if (included != null && included._includedFrom == this) {
          included.setImportPrecedence(precedence);
        }
      }
    }

    // Set import precedence for the stylesheet that imported this one
    if (_importedFrom != null) {
      if (_importedFrom.getImportPrecedence() < precedence) {
        final Parser parser = getParser();
        final int nextPrecedence = parser.getNextImportPrecedence();
        _importedFrom.setImportPrecedence(nextPrecedence);
      }
    }
    // Set import precedence for the stylesheet that included this one
    else if (_includedFrom != null) {
      if (_includedFrom.getImportPrecedence() != precedence)
        _includedFrom.setImportPrecedence(precedence);
    }
  }
	public void WriteHeader(){
		out.println("\t\t"+Parser.StartingTag("ValidichroHeader"));
		out.println("\t\t\t"+Parser.Tag("Date",experiment_date));
		out.println("\t\t\t"+Parser.Tag("Generic",generic));
		//...
		out.println("\t\t"+Parser.EndingTag("ValidichroHeader"));
	}
Beispiel #5
0
 /**
  * Parses some String input without a supporting file, returning statements and comments.
  *
  * @param inputLines a list of lines of code
  */
 @VisibleForTesting
 Parser.ParseResult parseFileWithComments(String... inputLines) {
   ParserInputSource input = ParserInputSource.create(Joiner.on("\n").join(inputLines), null);
   return isSkylark
       ? Parser.parseFileForSkylark(input, eventHandler, new ValidationEnvironment(this))
       : Parser.parseFile(input, eventHandler, /*parsePython=*/ false);
 }
Beispiel #6
0
 @Test
 public void testBasic() throws Exception {
   Parser p = new Parser("hello");
   Node t = p.parse();
   assertEquals(Node.LITERAL, t.getType());
   assertEquals("hello", t.getValue());
 }
 /**
  * Delegates the call to the matching component parser.
  *
  * <p>Potential {@link RuntimeException}s, {@link IOException}s and {@link SAXException}s
  * unrelated to the given input stream and content handler are automatically wrapped into {@link
  * TikaException}s to better honor the {@link Parser} contract.
  */
 public void parse(
     InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context)
     throws IOException, SAXException, TikaException {
   Parser parser = getParser(metadata, context);
   TemporaryResources tmp = new TemporaryResources();
   try {
     TikaInputStream taggedStream = TikaInputStream.get(stream, tmp);
     TaggedContentHandler taggedHandler =
         handler != null ? new TaggedContentHandler(handler) : null;
     if (parser instanceof ParserDecorator) {
       metadata.add(
           "X-Parsed-By", ((ParserDecorator) parser).getWrappedParser().getClass().getName());
     } else {
       metadata.add("X-Parsed-By", parser.getClass().getName());
     }
     try {
       parser.parse(taggedStream, taggedHandler, metadata, context);
     } catch (RuntimeException e) {
       throw new TikaException("Unexpected RuntimeException from " + parser, e);
     } catch (IOException e) {
       taggedStream.throwIfCauseOf(e);
       throw new TikaException("TIKA-198: Illegal IOException from " + parser, e);
     } catch (SAXException e) {
       if (taggedHandler != null) taggedHandler.throwIfCauseOf(e);
       throw new TikaException("TIKA-237: Illegal SAXException from " + parser, e);
     }
   } finally {
     tmp.dispose();
   }
 }
Beispiel #8
0
 @Test
 public void parseAeqBandXgrtY() {
   assertTrue(parser.hasHigherPrecedence("=", "AND"));
   assertFalse(parser.hasHigherPrecedence("=", ">"));
   List<String> list = parser.toPrefix("a = b AND x > y");
   assertEquals(Arrays.asList("a", "b", "=", "x", "y", ">", "AND"), list);
 }
  @Test
  public void testParserAndGenerator() throws Exception {
    WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
    ByteBufferPool bufferPool = new MappedByteBufferPool();
    Generator gen = new Generator(policy, bufferPool);
    Parser parser = new Parser(policy, bufferPool);
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);

    String message = "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF";

    ByteBuffer out = bufferPool.acquire(8192, false);
    try {
      // Generate Buffer
      BufferUtil.flipToFill(out);
      WebSocketFrame frame = WebSocketFrame.text(message);
      out = gen.generate(frame);

      // Parse Buffer
      parser.parse(out);
    } finally {
      bufferPool.release(out);
    }

    // Validate
    capture.assertNoErrors();
    capture.assertHasFrame(OpCode.TEXT, 1);

    WebSocketFrame txt = capture.getFrames().get(0);
    Assert.assertThat("Text parsed", txt.getPayloadAsUTF8(), is(message));
  }
Beispiel #10
0
  public static void main(String[] args) {
    Parser p = new Parser("data/test_parser2.txt");

    for (String s = p.readSentence(); s != null; s = p.readSentence()) {
      System.out.println(s);
    }
  }
Beispiel #11
0
  public C parse(C commandInstance, String... args) {
    Preconditions.checkNotNull(args, "args is null");

    Parser parser = new Parser(metadata);
    ParseState state = parser.parse(args);

    CommandMetadata command = MetadataLoader.loadCommand(commandInstance.getClass());

    state = state.withCommand(command);

    validate(state);

    ImmutableMap.Builder<Class<?>, Object> bindings =
        ImmutableMap.<Class<?>, Object>builder().put(GlobalMetadata.class, metadata);

    if (state.getGroup() != null) {
      bindings.put(CommandGroupMetadata.class, state.getGroup());
    }

    bindings.put(CommandMetadata.class, command);

    C c =
        (C)
            ParserUtil.injectOptions(
                commandInstance,
                command.getAllOptions(),
                state.getParsedOptions(),
                command.getArguments(),
                state.getParsedArguments(),
                command.getMetadataInjections(),
                bindings.build());

    return c;
  }
Beispiel #12
0
  public static void main(String[] args) {
    String line = null;
    Board board = new Board(5, 5);

    Parser parser = new Parser(board);
    Scanner scanner = new Scanner(System.in);

    System.out.println("Toy Robot Simulator");
    System.out.println("Enter a command:");
    System.out.println(" EX: PLACE X,Y,NORTH|SOUTH|EAST|WEST");
    System.out.println(" EX: MOVE|LEFT|RIGHT|REPORT");
    System.out.println("Type QUIT to EXIT");

    do {
      System.out.print("Toy Robot>");
      line = scanner.nextLine();
      try {

        if ("QUIT".equalsIgnoreCase(line)) {
          break;
        } else {
          line = line.toUpperCase();
          String result = parser.input(line);
          if (result != null && !"".equals(result)) {
            System.out.println(result);
          }
        }
      } catch (Exception e) {
        e.printStackTrace();
      }

    } while (true);
  }
 //	Recebe a entrada (input) e envia ao parser para que este determine qual o tipo do comando
 // (CommandUse ou CommandPath)
 public void execute(String command)
     throws ParserConfigurationException, SAXException, IOException, SQLException,
         ClassNotFoundException {
   Parser parser = new Parser();
   parser.setCommand(command.trim());
   this.execute(parser.getCommand());
 }
Beispiel #14
0
  /** Parse the contents of the variable */
  public void parseContents(Parser parser) {
    // Parse 'name' and 'select' attributes plus parameter contents
    super.parseContents(parser);

    // Add a ref to this var to its enclosing construct
    SyntaxTreeNode parent = getParent();
    if (parent instanceof Stylesheet) {
      // Mark this as a global variable
      _isLocal = false;
      // Check if a global variable with this name already exists...
      Variable var = parser.getSymbolTable().lookupVariable(_name);
      // ...and if it does we need to check import precedence
      if (var != null) {
        final int us = this.getImportPrecedence();
        final int them = var.getImportPrecedence();
        // It is an error if the two have the same import precedence
        if (us == them) {
          final String name = _name.toString();
          reportError(this, parser, ErrorMsg.VARIABLE_REDEF_ERR, name);
        }
        // Ignore this if previous definition has higher precedence
        else if (them > us) {
          _ignore = true;
          return;
        } else {
          var.disable();
        }
        // Add this variable if we have higher precedence
      }
      ((Stylesheet) parent).addVariable(this);
      parser.getSymbolTable().addVariable(this);
    } else {
      _isLocal = true;
    }
  }
Beispiel #15
0
 public void parseStyleDeclaration(CSSStyleDeclaration sd, InputSource source) throws IOException {
   Stack nodeStack = new Stack();
   nodeStack.push(sd);
   CSSOMHandler handler = new CSSOMHandler(nodeStack);
   _parser.setDocumentHandler(handler);
   _parser.parseStyleDeclaration(source);
 }
 protected void collectSongs(File disc_dir) {
   File[] songs = disc_dir.listFiles();
   for (File song : songs) {
     SongInfo info = new SongInfo();
     boolean valid = false;
     Iterator<String> itr = FileAnalyzerSettings.FORMATS.iterator();
     while (!valid && itr.hasNext()) {
       String format = itr.next();
       try {
         Parser p = new Parser(format);
         Hashtable<String, Object> data = p.parse(song.getAbsolutePath());
         // reflect
         Iterator<Entry<String, Object>> data_itr = data.entrySet().iterator();
         while (data_itr.hasNext()) {
           Entry<String, Object> e = data_itr.next();
           Field f = SongInfo.class.getDeclaredField(e.getKey() + "_");
           f.set(info, e.getValue());
         }
         iterator_.organize(info);
         valid = true;
       } catch (Exception e) {
         // System.err.println("error: " + format);
       }
     }
   }
 }
Beispiel #17
0
  @Test
  public void testOptions() throws Exception {
    {
      Parser p = new Parser("%45x{'test '}");
      Node t = p.parse();
      KeywordNode witness = new KeywordNode("x");
      witness.setFormatInfo(new FormatInfo(45, Integer.MAX_VALUE));
      List<String> ol = new ArrayList<String>();
      ol.add("test ");
      witness.setOptions(ol);
      assertEquals(witness, t);
    }

    {
      Parser p = new Parser("%45x{a, b}");
      Node t = p.parse();
      KeywordNode witness = new KeywordNode("x");
      witness.setFormatInfo(new FormatInfo(45, Integer.MAX_VALUE));
      List<String> ol = new ArrayList<String>();
      ol.add("a");
      ol.add("b");
      witness.setOptions(ol);
      assertEquals(witness, t);
    }
  }
  static void testRuleDependency() {
    String prQuery =
        "Edge(int s,int t).\n"
            + "Foo(int a,int b).\n"
            + "Bar(int a,int b).\n"
            + "Zoo(int a,int b).\n"
            + "Foo(a,b) :- Bar(a,c), Zoo(b,c).\n"
            + "Bar(a,b) :- Bar(a,c), Zoo(b,c).\n"
            + "Edge(s,t) :- s=1, t=2 .\n";

    Parser p = new Parser(prQuery);
    p.parse();

    Analysis an = new Analysis(p);
    an.run();
    List<Epoch> strata = an.getEpochs();
    Assert.true_(strata.size() == 2, "Strata #:" + strata.size());

    List<Rule> rules = an.getRules();
    Rule fooRule = rules.get(0);
    Assert.true_(fooRule.name().startsWith("Foo"));
    Assert.true_(
        fooRule.getDependingRules().size() == 0, // no deps in the same strata
        "Foo deps #:" + fooRule.getDependingRules().size());

    Rule barRule = rules.get(1);
    Assert.true_(barRule.name().startsWith("Bar"));
    Assert.true_(barRule.getDependingRules().size() == 2);

    Rule edgeRule = rules.get(2);
    Assert.true_(edgeRule.name().startsWith("Edge"));
    Assert.true_(edgeRule.getDependingRules().size() == 0);
  }
Beispiel #19
0
  public C parse(C commandInstance, String... args) {
    Preconditions.checkNotNull(args, "args is null");

    Parser parser = new Parser();
    ParseState state = parser.parse(metadata, args);

    if (state.getCommand() == null) {
      if (state.getGroup() != null) {
        state = state.withCommand(state.getGroup().getDefaultCommand());
      } else {
        state = state.withCommand(metadata.getDefaultCommand());
      }
    }

    validate(state);

    CommandMetadata command = state.getCommand();

    return injectOptions(
        commandInstance,
        command.getAllOptions(),
        state.getParsedOptions(),
        command.getArguments(),
        state.getParsedArguments(),
        command.getMetadataInjections(),
        ImmutableMap.<Class<?>, Object>of(GlobalMetadata.class, metadata));
  }
  static void testEpochs() {
    String query =
        "SP(int n:0..2000000,int d).\n"
            + "Edge(int s,int t).\n"
            + "Foo(int n, int a).\n"
            + "SP(t,d) :- Edge(1,t), d=1 .\n"
            + "SP(t,$min(d)) :- SP(p,d1), Edge(p,t), d=d1+1 .\n"
            + "Foo(a, b) :- SP(a,b).\n"
            + "Edge(n1,n2) :- n1=2, n2=20.\n";
    Parser p = new Parser();
    p.parse(query);
    Analysis an = new Analysis(p);
    an.run();
    List<Epoch> epochs = an.getEpochs();

    Assert.true_(epochs.size() == 3);
    Epoch e = epochs.get(0);
    Assert.true_(e.getRuleCompNum() == 1);
    Rule edgeLoad = e.getRuleComps().get(0).get(0);
    Assert.true_(edgeLoad.getHead().name().equals("Edge"));

    e = epochs.get(1);
    Assert.true_(e.getRuleCompNum() == 1);
    Rule sp = e.getRuleComps().get(0).get(0);
    Assert.true_(sp.getHead().name().equals("SP") && sp.getHead().hasFunctionParam());
    e = epochs.get(2);
    Assert.true_(e.getRuleCompNum() == 1);
    Rule foo = e.getRuleComps().get(0).get(0);
    Assert.true_(foo.getHead().name().equals("Foo"));
  }
Beispiel #21
0
  public Worker(String url, boolean verbose) throws Exception {
    Document doc;
    doc = Jsoup.connect(url).get();
    // select anchors with href only
    Elements links = doc.select("a[href]");
    String l_Href;
    String host;
    int linksNum;
    Parser parser;
    for (Element link : links) {
      // absolute = http:// added
      l_Href = link.attr("abs:href");
      if (!l_Href.isEmpty()) {
        parser = new Parser(l_Href);
        host = parser.getHost();
        // if tempStats contains the url, add one to the value
        if (tempStats.containsKey(host)) {
          linksNum = tempStats.get(host);
          tempStats.put(host, linksNum += 1);
        }
        // if it doesn't, add it

        else {
          tempStats.put(host, 1);
        }
        // parse the url
        tempQueue.add(parser.getURL());
      }
    }
    if (verbose) {
      System.out.println(
          Thread.currentThread().getName() + " : " + tempQueue.size() + " links from " + url);
    }
  }
  static void testLocationOpInPredicate() {
    String query =
        "Edge(int a:0..100, (int b)). \n" + "Attr(int a,int b).\n" + "Attr(a,b) :- Edge(a,b).\n";
    Parser p = new Parser();
    TestAnalysis an;
    try {
      p.parse(query);
      an = new TestAnalysis(p);
      an.setDistributed();
      an.run();
      Assert.die("should not reach here");
    } catch (ParseException e) {
      /* expected */
    }

    query =
        "Edge(int a:0..100, (int b)). \n" + "Attr(int a,int b).\n" + "Attr(a, b) :- Edge(a, b).\n";
    p = new Parser();
    try {
      p.parse(query);
      an = new TestAnalysis(p);
      an.setDistributed();
      an.run();
      Assert.die("should not reach here");
    } catch (ParseException e) {
      /* expected */
    }
  }
Beispiel #23
0
 public static void main(String[] args) {
   Parser parser = new Parser();
   Label labels = new Label();
   String asmbly;
   int pc = 100;
   try {
     BufferedReader br = new BufferedReader(new FileReader(args[0]));
     String line;
     while ((line = br.readLine()) != null) {
       if (line.contains(":")) labels.fillLabels(line, pc);
       pc++;
     }
   } catch (Exception e) {
     System.out.println("Failed first");
   }
   try {
     BufferedReader br = new BufferedReader(new FileReader(args[0]));
     PrintWriter writer = new PrintWriter("output", "UTF-8");
     String line;
     while ((line = br.readLine()) != null) {
       asmbly = parser.parseLine(line, labels);
       writer.println(asmbly);
     }
     writer.close();
   } catch (Exception e) {
     System.out.println("Failed second");
   }
 }
  static void testVarResolution() {
    String simpleQuery =
        "Edge(int s,(int t)).\n"
            + "Foaf(int a,int b).\n"
            + "Foo(int a,int b).\n"
            + "Foaf(n1,n3) :- Foo(n1,n2), Edge(n2,n3), n4=n1*(n2+1).\n";
    Parser p = new Parser(simpleQuery);
    p.parse();
    Analysis an = new Analysis(p);
    an.run();

    Rule r = p.getRules().get(0);
    Set<Variable> resolvedVars[] = Analysis.getResolvedVars(r);
    Assert.equals(resolvedVars.length, 4);
    Assert.true_(resolvedVars[0].isEmpty());
    Assert.true_(resolvedVars[1].contains(new Variable("n1", int.class)));
    Assert.true_(resolvedVars[1].contains(new Variable("n2", int.class)));
    Assert.true_(!resolvedVars[1].contains(new Variable("n3", int.class)));
    Assert.true_(!resolvedVars[1].contains(new Variable("n3", int.class)));

    Assert.true_(!resolvedVars[2].contains(new Variable("n4", int.class)));

    Assert.true_(resolvedVars[3].contains(new Variable("n1", int.class)));
    Assert.true_(resolvedVars[3].contains(new Variable("n2", int.class)));
    Assert.true_(resolvedVars[3].contains(new Variable("n3", int.class)));
    Assert.true_(resolvedVars[3].contains(new Variable("n4", int.class)));
  }
Beispiel #25
0
 private NodeList getMatchingTags(NodeFilter filter) throws Exception {
   String html = examiner.html();
   Parser parser = new Parser(new Lexer(new Page(html)));
   NodeList list = parser.parse(null);
   NodeList matches = list.extractAllNodesThatMatch(filter, true);
   return matches;
 }
Beispiel #26
0
  public void test13425() {
    Options options = new Options();
    Option oldpass =
        OptionBuilder.withLongOpt("old-password")
            .withDescription("Use this option to specify the old password")
            .hasArg()
            .create('o');
    Option newpass =
        OptionBuilder.withLongOpt("new-password")
            .withDescription("Use this option to specify the new password")
            .hasArg()
            .create('n');

    String[] args = {"-o", "-n", "newpassword"};

    options.addOption(oldpass);
    options.addOption(newpass);

    Parser parser = new PosixParser();

    try {
      CommandLine line = parser.parse(options, args);
    }
    // catch the exception and leave the method
    catch (Exception exp) {
      assertTrue(exp != null);
      return;
    }
    fail("MissingArgumentException not caught.");
  }
Beispiel #27
0
 /**
  * Evaluates expression.
  *
  * @param expression expression
  * @param arg arguments
  * @return result
  * @throws MathException if some error occurs
  */
 private static double evaluate(String expression, Map<String, Double> arg) throws MathException {
   Parser parser = new Parser();
   if (arg != null) {
     return parser.compile(expression, arg.keySet()).evaluate(arg);
   }
   return parser.compile(expression).evaluate(arg);
 }
Beispiel #28
0
  public static void readFB2(String path) {

    Parser parser = new InstantParser();
    EBook ebook = parser.parse(path);

    if (ebook.isOk) {
      MyReader.windowArea.append(ebook.annotation);

      System.out.println(ebook.annotation);
      System.out.println(ebook.doExtractCover);
      System.out.println(ebook.encoding);
      System.out.println(ebook.fileName);
      System.out.println(ebook.isOk);
      System.out.println(ebook.language);
      System.out.println(ebook.sequenceName);
      System.out.println(ebook.sequenceNumber);
      System.out.println(ebook.srcLanguage);
      System.out.println(ebook.title);
      System.out.println(ebook.authors);
      System.out.println(ebook.cover);
      System.out.println(ebook.epubGenres);
      System.out.println(ebook.fb2Genres);
      System.out.println(ebook.format);
      System.out.println(ebook.translators);
    }
  }
Beispiel #29
0
  /* input defaults to reading the Fakefile, cwd to "." */
  public void make(InputStream input, File cwd, String[] args) {
    try {
      Parser parser = parse(input, cwd);

      // filter out variable definitions
      int firstArg = 0;
      while (firstArg < args.length && args[firstArg].indexOf('=') >= 0) firstArg++;

      List<String> list = null;
      if (args.length > firstArg) {
        list = new ArrayList<String>();
        for (int i = firstArg; i < args.length; i++) list.add(args[i]);
      }
      Rule all = parser.parseRules(list);

      for (int i = 0; i < firstArg; i++) {
        int equal = args[i].indexOf('=');
        parser.setVariable(args[i].substring(0, equal), args[i].substring(equal + 1));
      }

      for (Rule rule : all.getDependenciesRecursively())
        if (rule.getVarBool("rebuild")) rule.clean(false);

      String parallel = all.getVar("parallel");
      if (parallel != null) all.makeParallel(Integer.parseInt(parallel));
      else all.make();
    } catch (FakeException e) {
      System.err.println(e);
      System.exit(1);
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    }
  }
Beispiel #30
0
  @Test
  public void testTokenize() {
    ArrayList<String> tok = new ArrayList<String>();

    // Test the basic delimiter handling, runs of [ ,\t] are one delimiter
    tok.clear();
    Parser.tokenize(tok, "A A,A\tA  A,,A\t\tA ,\tA");
    tokenMatch(tok, "A", "A", "A", "A", "A", "A", "A", "A");

    tok.clear();
    Parser.tokenize(tok, " A A,A\tA  A,,A\t\tA ,\tA\t,  ");
    tokenMatch(tok, "A", "A", "A", "A", "A", "A", "A", "A");

    tok.clear();
    Parser.tokenize(tok, "OBJECT KEYWORD{ ARG}KEYWORD\t{ARG ARG,}");
    tokenMatch(tok, "OBJECT", "KEYWORD", "{", "ARG", "}", "KEYWORD", "{", "ARG", "ARG", "}");

    tok.clear();
    Parser.tokenize(tok, "OBJECT KEYWORD{ 'ARG  '}KEYWORD\t{ARG' ARG',}");
    tokenMatch(tok, "OBJECT", "KEYWORD", "{", "ARG  ", "}", "KEYWORD", "{", "ARG", " ARG", "}");

    tok.clear();
    Parser.tokenize(tok, "OBJECT KEYWORD{ ARG }\"FOO ,\t     ");
    tokenMatch(tok, "OBJECT", "KEYWORD", "{", "ARG", "}", "\"FOO ,\t     ");
  }