Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

50
LINES

< > TinyBrain | #1008908 - ctxParseExpression

JavaX fragment (include)

static CtxExpr ctxParseExpression(S exp) {
  Explain e = explain(exp, ctxParsingRules(), "exp");
  if (e == null) fail("Couldn't parse expression: " + exp);
  ret ctxParseExpression(e);
}

static CtxExpr ctxParseExpression(Explain exp) {
  try {
    if (exp.is("bracket")) {
      CtxExpr e = ctxParseExpression(exp.sub(0));
      if (e << CtxConst)
        ret new CtxRegexp(e/CtxConst.value);
      ret e;
    }
    if (exp.is("fcall")) {
      CtxFCall call = new(exp.str(0));
      for (int i = 1; i < l(exp.subs); i++)
        call.add(ctxParseSub(exp, i));
      ret call;
    }
    if (exp.is("and"))
      ret new CtxAnd(ctxParseSub(exp, 0),
        ctxParseSub(exp, 1));
    if (exp.is("or"))
      ret new CtxOr(ctxParseSub(exp, 0),
        ctxParseSub(exp, 1));
    if (exp.is("neg"))
      ret new CtxNeg(ctxParseSub(exp, 0));
    if (exp.is("int"))
      ret new CtxConst(exp.tok().get(1));
    if (exp.is("quoted"))
      ret new CtxConst(unquoteCtx(exp.tok().get(1)));
      //ret new CtxRegexp(unquoteCtx(exp.tok().get(1)));
    //if (exp.is("id"))
    if (l(exp.tok()) == 3 && isIdentifier(exp.tok().get(1)))
      ret new CtxVar(exp.tok().get(1));
    if (exp.is("comp"))
      ret new CtxComp(ctxParseSub(exp, 0), ctxParseSub(exp, 1));
    if (exp.is("uneq"))
      ret new CtxUneq(ctxParseSub(exp, 0), ctxParseSub(exp, 1));
    if (exp.is("match"))
      ret new CtxMatch(ctxParseSub(exp, 0), unquoteCtx(exp.str(1)));
    if (exp.singleEqualChild())
      ret ctxParseExpression(exp.sub(0));
  } catch e {
    printExplainTree(exp);
    throw rethrow(e);
  }
  throw todo(exp.className() + " - " + quote(exp.string()));
}

download  show line numbers  debug dex   

Travelled to 2 computer(s): cfunsshuasjs, tvejysmllsmz

No comments. add comment

Snippet ID: #1008908
Snippet name: ctxParseExpression
Eternal ID of this version: #1008908/22
Text MD5: dad46f875205875002f2e3af43100c50
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-07-01 19:04:38
Source code size: 1690 bytes / 50 lines
Pitched / IR pitched: No / No
Views / Downloads: 85 / 116
Version history: 21 change(s)
Referenced in: [show]