Package org.rythmengine.internal

Examples of org.rythmengine.internal.IContext


    private static Map<String, Pattern> patterns = new HashMap<String, Pattern>();

    @Override
    public Token go() {
        IContext ctx = ctx();
        if (ctx.insideDirectiveComment()) {
            return null;
            //raiseParseException("directive comment not closed");
        }
        ICodeType type = ctx.peekCodeType();
        while (null != type) {
            String sCommentStart = type.commentStart();
            if (!S.empty(sCommentStart)) {
                sCommentStart = S.escapeRegex(sCommentStart).toString();
                // try <!-- @ first
                String s = "(" + sCommentStart + "\\s*" + ")" + ctx.getDialect().a() + ".*";
                Pattern p = patterns.get(s);
                if (null == p) {
                    p = Pattern.compile(s, Pattern.DOTALL);
                    patterns.put(s, p);
                }
                Matcher m = p.matcher(remain());
                if (m.matches()) {
                    s = m.group(1);
                    ctx.step(s.length());
                    ctx.enterDirectiveComment();
                    return Token.EMPTY_TOKEN;
                }
                // try <!-- }
                s = "(" + sCommentStart + "\\s*)\\}.*";
                p = patterns.get(s);
                if (null == p) {
                    p = Pattern.compile(s, Pattern.DOTALL);
                    patterns.put(s, p);
                }
                m = p.matcher(remain());
                if (m.matches()) {
                    s = m.group(1);
                    ctx.step(s.length());
                    ctx.enterDirectiveComment();
                    return Token.EMPTY_TOKEN;
                }
            }
            type = type.getParent();
        }
View Full Code Here


    private static Map<String, Pattern> patterns = new HashMap<String, Pattern>();

    @Override
    public Token go() {
        IContext ctx = ctx();
        ICodeType curType = ctx.peekCodeType();
        if (curType.allowedExternalTypes().isEmpty()) return null;

        String remain = ctx.getRemain();

        String blockEnd = curType.blockEnd();
        if (null == blockEnd) {
            logger.warn("null block end found for type[%s]", curType);
            return null;
        }

        Pattern p = patterns.get(blockEnd);
        if (null == p) {
            p = Pattern.compile(blockEnd, Pattern.DOTALL);
            patterns.put(blockEnd, p);
        }
        Matcher m = p.matcher(remain);
        if (m.matches()) {
            String matched = m.group(1);
            ctx.step(matched.length());
            ctx.popCodeType();
            String s = String.format("p(\"%s\");__ctx.popCodeType();", matched, curType);
            return new CodeToken(s, ctx);
        }

        return null;
View Full Code Here

        super(context);
    }

    @Override
    public Token go() {
        IContext ctx = ctx();
        //if (ctx.currentBlock() == null) return null;
        Regex r = new Regex(String.format(PTN, a(), a()));
        if (!r.search(ctx.getRemain())) return null;
        if (!ctx.getDialect().enableScripting()) {
            throw new TemplateParser.ScriptingDisabledException(ctx);
        }
        String s = r.stringMatched(1);
        int curLine = ctx.currentLine();
        ctx.step(s.length());
        s = r.stringMatched(2);
        s = s.substring(1); // strip left "{"
        s = s.substring(0, s.length() - 1); // strip right "}"
        r = new Regex(".*[ \\t\\n\\r\\}]+if[ \\t]*\\(.*");
        boolean hasIfStatement = r.search(" " + s);
View Full Code Here

        P = pattern("\\n?[ \\t\\x0B\\f]*%s(%s)(\\s*|\\(|\\{).*", a(), Patterns.VarName);
    }
   
    public F.T2<IParser, Token> go2() {
        DialectBase d = (DialectBase) dialect();
        IContext c = ctx();
        Matcher m = P.matcher(remain());
        if (m.matches()) {
            String s = m.group(1);
            IParser p = d.createBuildInParser(s, c);
            if (null != p) {
View Full Code Here

     */
    private static final String PTN = "(%s%s.*?|.*?)([\\n\\r@\\<\\#\\$\\&\\{\\}\\-\\*\\/].*|$)";

    @Override
    public Token go() {
        IContext ctx = ctx();
        String s = ctx.getRemain();
        if (s.length() == 0) {
            return Token.EMPTY_TOKEN;
        }
        String a = a();
        Pattern p = Pattern.compile(String.format(PTN, a, a, a),
                Pattern.DOTALL);
        Matcher m = p.matcher(s);
        if (!m.matches()) {
            return null;
        }
        s = m.group(1);
        if (s.length() == 0) {
            return null;
        }
        ctx.step(s.length());
        s = s.replace(String.format("%s%s", a, a), a).replace("\\", "\\\\");
        if ("".equals(s)) {
            return Token.EMPTY_TOKEN;
        } else {
            return new Token.StringToken(s, ctx);
View Full Code Here

        if (iterable.contains("..") || iterable.contains(" to ") || iterable.contains(" till ")) {
            iterable = "org.rythmengine.utils.Range.valueOf(\"" + iterable + "\")";
        }
        this.iterable = iterable;
        //openPos = context.cursor();
        IContext ctx = context;
        ctx.pushBreak(IContext.Break.BREAK);
        ctx.pushContinue(IContext.Continue.CONTINUE);
        CodeBuilder cb = context.getCodeBuilder();
        boolean isBasic = ctx.getDialect() instanceof BasicRythm;
        if (S.isEmpty(type) || "Object".equals(type)) {
            String itrType = cb.getRenderArgType(iterable);
            if (null != itrType) {
                Regex r = new Regex(".*((?@<>))");
                if (r.search(itrType)) {
                    type = r.stringMatched(1);
                    this.type = S.strip(type, "<", ">");
                    boolean key = iterable.endsWith("keySet()");
                    boolean val = iterable.endsWith("values()");
                    if (key || val) {
                        r = new Regex("([a-zA-Z0-9\\[\\]_]+(?@<>)?)\\s*\\,\\s*([a-zA-Z0-9\\[\\]_]+(?@<>)?)");
                        if (r.search(this.type)) {
                            if (key) this.type = r.stringMatched(1);
                            else this.type = r.stringMatched(2);
                        } else {
                            throw new ParseException(ctx.getEngine(), ctx.getTemplateClass(), line, "Invalid for loop iterator type declaration: %s", itrType);
                        }
                    }
                } else {
                    if (itrType.endsWith("]")) {
                        int pos = itrType.lastIndexOf("[");
View Full Code Here

        super(context);
    }

    @Override
    public Token go() {
        IContext ctx = ctx();
        IBlockHandler bh = ctx.currentBlock();
        if (null == bh) return null;
        String remain = remain();
        String s;
        if ("@".equals(remain)) {
            s = remain;
        } else {
            Pattern p = Pattern.compile(String.format(PTN2, a(), a(), a()), Pattern.DOTALL);
            Matcher m = p.matcher(remain);
            if (!m.matches()) {
                p = Pattern.compile(String.format(PTN, a()), Pattern.DOTALL);
                m = p.matcher(remain);
                if (!m.matches()) {
                    return null;
                }
            }
            s = m.group(1);
        }
        // keep ">" or "]" for case like <a id=".." @if (...) class="error" @>
        if (s.endsWith(">") || s.endsWith("]") || s.endsWith("\n")) s = s.substring(0, s.length() - 1);
        ctx.step(s.length());
        boolean hasLineBreak = s.contains("\\n") || s.contains("\\r");
        try {
            s = ctx.closeBlock();
            if (hasLineBreak) s = s + "\n"; // fix #53
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        CodeToken ct = new CodeToken(s, ctx);
        if (!(bh instanceof BlockToken.LiteralBlock)) {
            String bhCls = bh.getClass().getName();
            if (bhCls.contains("ForEach") || bhCls.contains("ElseFor") || bhCls.contains("Assign")) {
                ctx.getCodeBuilder().removeSpaceTillLastLineBreak(ctx);
                ct.removeNextLineBreak = true;
            } else {
                ctx.getCodeBuilder().removeSpaceToLastLineBreak(ctx);
            }
        }
        return ct;
    }
View Full Code Here

    private static Map<String, Pattern> patterns = new HashMap<String, Pattern>();

    @Override
    public Token go() {
        IContext ctx = ctx();
        if (!ctx.insideDirectiveComment()) {
            return null;
        }
        ICodeType type = ctx.peekCodeType();
        while (null != type) {
            String s = type.commentEnd();
            if (!S.empty(s)) {
                s = S.escapeRegex(s).toString();
                s = "(\\s*" + s + ")" + ".*";
                Pattern p = patterns.get(s);
                if (null == p) {
                    p = Pattern.compile(s, Pattern.DOTALL);
                    patterns.put(s, p);
                }
                Matcher m = p.matcher(remain());
                if (m.matches()) {
                    s = m.group(1);
                    ctx.step(s.length());
                    ctx.leaveDirectiveComment();
                    return Token.EMPTY_TOKEN;
                }
            }
            type = type.getParent();
        }
View Full Code Here

    private static Map<String, Pattern> patterns = new HashMap<String, Pattern>();

    @Override
    public Token go() {
        IContext ctx = ctx();
        ICodeType curType = ctx.peekCodeType();
        if (!curType.allowInternalTypeBlock()) return null;

        String remain = ctx.getRemain();
        Iterable<ICodeType> types = ctx.getEngine().extensionManager().templateLangs();

        for (ICodeType type : types) {
            if (type.allowedExternalTypes().contains(curType)) {
                String blockStart = type.blockStart();
                if (null == blockStart) {
                    logger.warn("null block start found for lang[%s] inside lang[%s]", type, curType);
                    continue;
                }

                Pattern pStart = patterns.get(blockStart);
                if (null == pStart) {
                    pStart = Pattern.compile(blockStart, Pattern.DOTALL);
                    patterns.put(blockStart, pStart);
                }
                Matcher m = pStart.matcher(remain);
                if (m.matches()) {
                    ctx.pushCodeType(type);
                    String matched = m.group(1);
                    ctx.step(matched.length());
                    String s = matched;
                    if (matched.indexOf('@') > -1) {
                        // process internal template
                        char ch = s.charAt(0);
                        s = s.substring(1); //prevent CodeTypeBlockStartSensor to sense it again
                        String code = ctx.getCodeBuilder().addInlineInclude(s, ctx.currentLine());
                        if (matched.endsWith("\n")) {
                            code = code + ";p(\"\\n\");";
                        }
                        s = String.format("p('%s');", String.valueOf(ch)) + code + String.format(";__ctx.pushCodeType(%s);", type.newInstanceStr());
                    } else {
View Full Code Here

TOP

Related Classes of org.rythmengine.internal.IContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.