public Syntax getSyntaxRule(CTX ctx, List<Token> tls, int s, int e) { Token tk = tls.get(s); if (tk.kw.equals(KW.Type)) { tk = lookAhead(ctx, tls, s + 1, e); if (tk != null && (tk.tt == TK.SYMBOL || tk.tt == TK.USYMBOL)) { tk = lookAhead(ctx, tls, s + 2, e); if (tk != null && (tk.tt == TK.AST_PARENTHESIS || tk.kw.equals(KW.DOT))) { return syntax(ctx, KW.StmtMethodDecl); // } return syntax(ctx, KW.StmtTypeDecl); // } return syntax(ctx, KW.Expr); // expression } Syntax syn = syntax(ctx, tk.kw); if (syn.syntaxRuleNULL == null) { ctx.DBG_P( "kw='%s', %d, %d", syn.kw, 0, 0); // TODO syn.ParseExpr == kmodsugar.UndefinedParseExpr, kmodsugar.UndefinedExprTyCheck // == syn.ExprTyCheck); int i; for (i = s + 1; i < e; i++) { tk = tls.get(i); syn = syntax(ctx, tk.kw); if (syn.syntaxRuleNULL != null && syn.priority > 0) { ctx.SUGAR_P(System.out, tk.uline, tk.lpos, "binary operator syntax kw='%s'", syn.kw); return syn; } } return syntax(ctx, KW.Expr); } return syn; }
public void tokenize(CTX ctx, String source, long uline, List<Token> toks) { int i, pos = toks.size(); TEnv tenv = new TEnv(source, uline, toks, 4, this); Tokenizer.tokenize(ctx, tenv); if (uline == 0) { for (i = pos; i < toks.size(); i++) { toks.get(i).uline = 0; } } }
public KObject eval(CTX ctx, String script, long uline) { ctx.gamma = new Gamma(); ctx.gamma.ks = this; List<Token> tls = new ArrayList<Token>(); int pos = tls.size(); tokenize(ctx, script, uline, tls); Block bk = Parser.newBlock(ctx, this, null, tls, pos, tls.size(), ';'); KArray.clear(tls, pos); return evalBlock(ctx, bk); }
private int findTopCh(CTX ctx, List<Token> tls, int s, int e, int tt, int closech) { for (int i = s; i < e; i++) { Token tk = tls.get(i); if (tk.tt == tt && tk.text.charAt(0) == closech) return i; } return e; }
private boolean makeSyntaxRule(CTX ctx, List<Token> tls, int s, int e, List<Token> adst) { String nameid = null; for (int i = s; i < e; i++) { Token tk = tls.get(i); if (tk.tt == TK.INDENT) continue; if (tk.tt == TK.TEXT) { int[] ia = new int[] {i}; if (checkNestedSyntax(ctx, tls, ia, e, TK.AST_PARENTHESIS, '(', ')') || checkNestedSyntax(ctx, tls, ia, e, TK.AST_BRANCET, '[', ']') || checkNestedSyntax(ctx, tls, ia, e, TK.AST_BRACE, '{', '}')) { i = ia[0]; } else { tk.tt = TK.CODE; tk.kw = tk.text; } adst.add(tk); continue; } if (tk.tt == TK.SYMBOL || tk.tt == TK.USYMBOL) { if (i > 0 && tls.get(i - 1).topch == '$') { tk.kw = "$" + tk.text; tk.tt = TK.METANAME; if (nameid == null) nameid = tk.kw; tk.nameid = nameid; nameid = null; adst.add(tk); continue; } if (i + 1 < e && tls.get(i + 1).topch == ':') { nameid = tls.get(i).text; i++; continue; } } if (tk.tt == TK.OPERATOR) { int[] ia = new int[] {i}; if (checkNestedSyntax(ctx, tls, ia, e, TK.AST_OPTIONAL, '[', ']')) { adst.add(tk); i = ia[0]; continue; } if (tls.get(i).topch == '$') continue; } return false; } return true; }
private boolean checkNestedSyntax( CTX ctx, List<Token> tls, int[] s, int e, int tt, int opench, int closech) { int i = s[0]; Token tk = tls.get(i); String t = tk.text; if (t.length() == 1 && t.charAt(0) == opench) { int ne = findTopCh(ctx, tls, i + 1, e, tk.tt, closech); tk.tt = tt; if (tt >= 0 && tt < KW.TK_KW.length) tk.kw = KW.TK_KW[tt]; tk.sub = new ArrayList<Token>(); tk.topch = opench; tk.closech = closech; makeSyntaxRule(ctx, tls, i + 1, ne, tk.sub); s[0] = ne; return true; } return false; }
private Token lookAhead(CTX ctx, List<Token> tls, int s, int e) { return (s < e) ? tls.get(s) : null /*K_NULLTOKEN*/; }
private void parseSyntaxRule(CTX ctx, String rule, long uline, List<Token> a) { List<Token> tls = new ArrayList<Token>(); tokenize(ctx, rule, uline, tls); makeSyntaxRule(ctx, tls, 0, tls.size(), a); }