示例#1
0
  public RegexLexer parse(String rbnfCode) {
    if (rbnfCode == null) {
      throw new IllegalArgumentException("rbnfCode==null");
    }

    RBNFParser parser = new RBNFParser(rbnfCode);
    RBNF rbnf = parser.parse();
    if (rbnf == null) return null;

    RegexLexer lexer = new RegexLexer();

    List<Rule> rules = rbnf.getRules();
    for (Rule r : rules) {
      RegexTokenParser tokParser = rule(r, rbnf);
      if (tokParser != null) {
        lexer.getParsers().add(tokParser);
      }
    }

    return lexer;
  }
示例#2
0
  protected String id(Identifier id, RBNF rbnf) {
    String sid = id.getMatchedText();
    if (sid != null) {
      Rule r = rbnf.getRuleById(sid);
      if (r != null) {
        TreeNode ex = r.getExpression();
        if (ex != null) {
          String s = expression(ex, rbnf);
          return s;
        }
      }

      if (sid.equals("any")) return ".";
      if (sid.equals("letter")) return "\\p{Alpha}";
      if (sid.equals("digit")) return "\\p{Digit}";
      if (sid.equals("endl")) return "\\r?\\n";
      if (sid.equals("ws")) return "\\s";
    }
    return null;
  }