private List<Regex> regexPartial(SourcePosition pos) throws JmoException { List<Regex> regexList = new ArrayList<Regex>(); while (true) { Token t = buf.getToken(0); if (t == null) { break; } if (t.getType() == TokenType.LBRACE) { // { found buf.takeToken(); Regex rx = varPartial(); expect(TokenType.RBRACE); regexList.add(rx); } else { String str = this.readUntil(TokenType.OR, TokenType.LBRACE); Regex literal = new LiteralPartial(str, pos); regexList.add(literal); Token current = buf.findValid(); if (current == null || current.getType() == TokenType.OR) { break; } } } return regexList; }
private String collectToEnd() { StringBuilder sb = new StringBuilder(); buf.findValid(); do { Token t = buf.getToken(0); if (t == null) break; sb.append(t.getText()); } while (buf.takeToken() != null); return sb.toString(); }
private String readUntil(TokenType... stopTypes) { StringBuilder sb = new StringBuilder(); while (true) { Token t = buf.getToken(0); if (t == null) { break; } boolean shouldStop = false; for (TokenType type : stopTypes) { if (t.getType() == type) { shouldStop = true; } } if (shouldStop) { break; } buf.takeToken(); sb.append(t.getText()); } return sb.toString(); }
private RegexExpressionNode regexNode(SourcePosition position) throws JmoException { List<Regex[]> regs = new ArrayList<Regex[]>(); while (true) { Token t = buf.getToken(0); if (t == null) { break; } if (t.getType() == TokenType.OR) { if (regs.size() == 0) { throw new ParseException("'|' must NOT be first element", buf.findValid().getPosition()); } buf.takeToken(); } List<Regex> rx = this.regexPartial(buf.currentPosition()); if (rx.size() == 0) { throw new ParseException("'|' has no succeed token", buf.currentPosition()); } regs.add(rx.toArray(new Regex[rx.size()])); } RegexExpression or = new RegexExpression(regs, position); return new RegexExpressionNode(position, or); }
private Regex varPartial() throws JmoException { VarsPartial varRegex = new VarsPartial(buf.currentPosition()); boolean hasNext = true; while (true) { Token next = buf.getToken(0); if (next == null) { throw new RuntimeException("expect } at line end"); } if (next.getType() == TokenType.RBRACE) { break; } else if (next.getType() == TokenType.OR) { buf.takeToken(); continue; } else if (next.getType() == TokenType.WHITE) { buf.takeToken(); continue; } else { if (next.getType() == TokenType.DOLLAR) { buf.takeToken(); next = buf.findValid(); } VarNode vn = this.varNode(); Token expNode = buf.findValid(); if (expNode == null) { throw new ParseException("'}' NOT found", next.getPosition()); } // var.0 不允许是 // var.0- may // var.1- no less than // var.2-4 [2,4]个 int min = -1, max = -1; if (expNode.getType() == TokenType.DOT) { buf.takeToken(); Token expNum = buf.findValid(); try { min = Integer.parseInt(expNum.getText()); } catch (Exception e) { throw new ParseException( "not a valid number min repeat number for " + vn + ", token is '" + expNum + "'", expNum.getPosition()); } buf.takeToken(); Token expMinus = buf.findValid(); if (expMinus.getType() == TokenType.MINUS) { buf.takeToken(); Token expMax = buf.findValid(); if (expMax.getType() == TokenType.RBRACE || expMax.getType() == TokenType.OR) { max = Integer.MAX_VALUE; } else { try { max = Integer.parseInt(expMax.getText()); } catch (Exception e) { throw new ParseException( "not a valid number max repeat number for " + vn + ", token is '" + expNum + "'", expMax.getPosition()); } buf.takeToken(); } } } varRegex.addVar(expNode.getPosition(), vn, min, max); hasNext = false; } } if (hasNext) { throw new ParseException("expect regex expression, but not found", buf.currentPosition()); } return varRegex; }
private void expectLineEnd() throws JmoException { Token t = buf.getToken(0); if (t != null) { throw new ParseException("expect to lineEnd but get token " + t, t.getPosition()); } }