private Node parseBranch(final TokenType term) { Node node = parseExp(term); if (token.type == TokenType.EOT || token.type == term || token.type == TokenType.ALT) { return node; } final ConsAltNode top = ConsAltNode.newListNode(node, null); ConsAltNode t = top; while (token.type != TokenType.EOT && token.type != term && token.type != TokenType.ALT) { node = parseExp(term); if (node.getType() == NodeType.LIST) { t.setCdr((ConsAltNode)node); while (((ConsAltNode)node).cdr != null ) { node = ((ConsAltNode)node).cdr; } t = ((ConsAltNode)node); } else { t.setCdr(ConsAltNode.newListNode(node, null)); t = t.cdr; } } return top; }
private Node parseSubExp(final TokenType term) { Node node = parseBranch(term); if (token.type == term) { return node; } else if (token.type == TokenType.ALT) { final ConsAltNode top = ConsAltNode.newAltNode(node, null); ConsAltNode t = top; while (token.type == TokenType.ALT) { fetchToken(); node = parseBranch(term); t.setCdr(ConsAltNode.newAltNode(node, null)); t = t.cdr; } if (token.type != term) { parseSubExpError(term); } return top; } else { parseSubExpError(term); return null; //not reached } }
private Node parseBranch(TokenType term) { Node node = parseExp(term); if (token.type == TokenType.EOT || token.type == term || token.type == TokenType.ALT) { return node; } else { ConsAltNode top = ConsAltNode.newListNode(node, null); ConsAltNode t = top; while (token.type != TokenType.EOT && token.type != term && token.type != TokenType.ALT) { node = parseExp(term); if (node.getType() == NodeType.LIST) { t.setCdr((ConsAltNode)node); while (((ConsAltNode)node).cdr != null ) node = ((ConsAltNode)node).cdr; t = ((ConsAltNode)node); } else { t.setCdr(ConsAltNode.newListNode(node, null)); t = t.cdr; } } return top; } }
private Node parseSubExp(TokenType term) { Node node = parseBranch(term); if (token.type == term) { return node; } else if (token.type == TokenType.ALT) { ConsAltNode top = ConsAltNode.newAltNode(node, null); ConsAltNode t = top; while (token.type == TokenType.ALT) { fetchToken(); node = parseBranch(term); t.setCdr(ConsAltNode.newAltNode(node, null)); t = t.cdr; } if (token.type != term) parseSubExpError(term); return top; } else { parseSubExpError(term); return null; //not reached } }
@Override protected void compileAltNode(final ConsAltNode node) { ConsAltNode aln = node; int len = 0; do { len += compileLengthTree(aln.car); if (aln.cdr != null) { len += OPSize.PUSH + OPSize.JUMP; } } while ((aln = aln.cdr) != null); final int pos = codeLength + len; /* goal position */ aln = node; do { len = compileLengthTree(aln.car); if (aln.cdr != null) { addOpcodeRelAddr(OPCode.PUSH, len + OPSize.JUMP); } compileTree(aln.car); if (aln.cdr != null) { len = pos - (codeLength + OPSize.JUMP); addOpcodeRelAddr(OPCode.JUMP, len); } } while ((aln = aln.cdr) != null); }
private Node parseExpRepeat(final Node targetp, final boolean group) { Node target = targetp; while (token.type == TokenType.OP_REPEAT || token.type == TokenType.INTERVAL) { // repeat: if (target.isInvalidQuantifier()) { throw new SyntaxException(ERR_TARGET_OF_REPEAT_OPERATOR_INVALID); } final QuantifierNode qtfr = new QuantifierNode(token.getRepeatLower(), token.getRepeatUpper(), token.type == TokenType.INTERVAL); qtfr.greedy = token.getRepeatGreedy(); final int ret = qtfr.setQuantifier(target, group, env, chars, getBegin(), getEnd()); Node qn = qtfr; if (token.getRepeatPossessive()) { final EncloseNode en = new EncloseNode(EncloseType.STOP_BACKTRACK); // node_new_enclose en.setTarget(qn); qn = en; } if (ret == 0) { target = qn; } else if (ret == 2) { /* split case: /abc+/ */ target = ConsAltNode.newListNode(target, null); final ConsAltNode tmp = ((ConsAltNode)target).setCdr(ConsAltNode.newListNode(qn, null)); fetchToken(); return parseExpRepeatForCar(target, tmp, group); } fetchToken(); // goto re_entry } return target; }
private Node parseExpRepeatForCar(final Node top, final ConsAltNode target, final boolean group) { while (token.type == TokenType.OP_REPEAT || token.type == TokenType.INTERVAL) { // repeat: if (target.car.isInvalidQuantifier()) { throw new SyntaxException(ERR_TARGET_OF_REPEAT_OPERATOR_INVALID); } final QuantifierNode qtfr = new QuantifierNode(token.getRepeatLower(), token.getRepeatUpper(), token.type == TokenType.INTERVAL); qtfr.greedy = token.getRepeatGreedy(); final int ret = qtfr.setQuantifier(target.car, group, env, chars, getBegin(), getEnd()); Node qn = qtfr; if (token.getRepeatPossessive()) { final EncloseNode en = new EncloseNode(EncloseType.STOP_BACKTRACK); // node_new_enclose en.setTarget(qn); qn = en; } if (ret == 0) { target.setCar(qn); } else if (ret == 2) { /* split case: /abc+/ */ assert false; } fetchToken(); // goto re_entry } return top; }
private Node divideLookBehindAlternatives(final Node nodep) { Node node = nodep; final AnchorNode an = (AnchorNode)node; final int anchorType = an.type; Node head = an.target; Node np = ((ConsAltNode)head).car; swap(node, head); final Node tmp = node; node = head; head = tmp; ((ConsAltNode)node).setCar(head); ((AnchorNode)head).setTarget(np); np = node; while ((np = ((ConsAltNode)np).cdr) != null) { final AnchorNode insert = new AnchorNode(anchorType); insert.setTarget(((ConsAltNode)np).car); ((ConsAltNode)np).setCar(insert); } if (anchorType == AnchorType.LOOK_BEHIND_NOT) { np = node; do { ((ConsAltNode)np).toListNode(); /* alt -> list */ } while ((np = ((ConsAltNode)np).cdr) != null); } return node; }