Package com.lastcalc

Examples of com.lastcalc.TokenList$TLPos


    final CombinedParserPickerFactory f = new CombinedParserPickerFactory(f1, f2);

    final BacktrackingParseEngine pe = new BacktrackingParseEngine(f);

    final ParserContext context = new ParserContext(pe, Long.MAX_VALUE);
    final TokenList result = pe.parseAndGetLastStep(new TokenList.SimpleTokenList("1", "1", "2", "2", "3", "3",
        "4", "4"),
        context);

    Assert.assertEquals(TokenList.createD("one", "two", "three", "four"), result);
  }
View Full Code Here


 
  public void hookedUpProperly(){
   
    final SequentialParser sp=SequentialParser.create();
   
    TokenList pr=sp.parseNext(TokenList.createD("6","!"));
   
    Assert.assertEquals(1,pr.size());
    Assert.assertEquals(LargeInteger.valueOf(720), pr.get(0));
   
  }
View Full Code Here

    public abstract ParseStep pickNext(ParserContext context, ParseStep previous);

    protected ParseStep getNext(final ParserContext context,
        final Iterable<Parser> parsers,
        final ParseStep previous) {
      final TokenList input = previous.result.output;
      for (final Parser candidate : parsers) {
        int sPos = -1;
        final Attempt attempt = new Attempt(input, candidate);
        // Check to see if we've tried applying this parser to these
        // input tokens before
View Full Code Here

      final List<Object> result = Lists.newArrayListWithCapacity(after.size() + 2);
      final boolean useBrackets = tokens.size() > template.size();
      if (useBrackets) {
        result.add("(");
      }
      final TokenList input = tokens.subList(templatePos, templatePos + template.size());
      final Map<String, Object> varMap = Maps.newHashMapWithExpectedSize(variables.size());
      try {
        bind(before, input, variables, varMap);
        for (final Object o : after) {
          final Object val = varMap.get(o);
          if (val != null) {
            result.add(val);
          } else {
            result.add(o);
          }
        }
      } catch (final BindException e) {
        return ParseResult.fail();
      }
      if (useBrackets) {
        result.add(")");
      }
      final TokenList resultTL = TokenList.create(result);
      final TokenList flattened = PreParser.flatten(resultTL);
      return ParseResult.success(
          tokens.replaceWithTokenList(templatePos, templatePos + template.size(), flattened),
          Math.min(0, -flattened.size())
          );
    }
View Full Code Here

    if (datastructure instanceof List) {
      if (udp.getTemplate().size() != 2)
        return ParseResult.fail();
      final List<Object> list = (List<Object>) datastructure;
      for (final Object o : list) {
        TokenList r = udp.parse(TokenList.createD(ret, o), 0).output;
        r = context.parseEngine.parseAndGetLastStep(r, context);
        if (r.size() != 1)
          return ParseResult.fail();
        else {
          ret = r.get(0);
        }
      }
    } else
      return ParseResult.fail();
    return ParseResult.success(tokens.replaceWithTokenList(templatePos, templatePos + template.size(),
View Full Code Here

    return TokenList.create(ret);
  }

  private static void flattenTo(final Object obj, final List<Object> output) {
    if (obj instanceof TokenList) {
      final TokenList sts = (TokenList) obj;
      if (sts.size() == 1) {
        flattenTo(sts.get(0), output);
      } else {
        for (final Object o : sts) {
          flattenTo(o, output);
        }
      }
View Full Code Here

  @Override
  public ParseResult parse(final TokenList tokens, final int templatePos, final ParserContext context) {
    final UserDefinedParser function = (UserDefinedParser) tokens.get(templatePos + 1);
    if (2 + templatePos + function.getTemplate().size() > tokens.size())
      return ParseResult.fail();
    final TokenList input = tokens.subList(templatePos + 3, templatePos + 3 + function.getTemplate().size());
    if (function.matchTemplate(input) != 0)
      return ParseResult.fail();
    final ParseResult parse = function.parse(input, 0, context);
    if (parse.isSuccess())
      return ParseResult.success(tokens.replaceWithTokenList(templatePos, templatePos + 3
View Full Code Here

    // Identify the beginning and end of the UDPP
    final int start = PreParser.findEdgeOrObjectBackwards(tokens, templatePos, null);
    final int end = PreParser.findEdgeOrObjectForwards(tokens, templatePos, null) + 1;

    final TokenList before = tokens.subList(start, templatePos);

    if (before.isEmpty())
      return ParseResult.fail();

    final TokenList after = PreParser.flatten(tokens.subList(templatePos + 1, end));

    if (after.isEmpty())
      return ParseResult.fail();

    final Set<String> variables = Sets.newHashSet();

    for (final Object o : PreParser.flatten(before)) {
View Full Code Here

        return ParseResult.fail();
      final List<Object> list = (List<Object>) datastructure;
      ret = Lists.newArrayListWithCapacity(list.size() * 4);
      ret.add("[");
      for (final Object o : list) {
        final TokenList r = context.parseEngine.parseAndGetLastStep(udp.parse(TokenList.createD(o), 0).output,
            context);
        Iterables.addAll(ret, r);
        ret.add(",");
      }
      ret.set(ret.size() - 1, "]"); // Overwrite last comma
View Full Code Here

          lineNumber.text(lineNo+".");
         
          final Element question = lineEl.appendElement("div").attr("class", "question")
              .attr("contentEditable", "true");
          question.text(qa.question);
          final TokenList strippedAnswer = sp.stripUDF(qa.answer);
          final AnswerType aType = WorksheetServlet.getAnswerType(strippedAnswer);
          if (aType.equals(AnswerType.NORMAL)) {
            lineEl.appendElement("div").attr("class", "equals").text("=");
            lineEl.appendElement("div").attr("class", "answer")
            .html(Renderers.toHtml("/", strippedAnswer).toString());
View Full Code Here

TOP

Related Classes of com.lastcalc.TokenList$TLPos

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.