Package com.stevesoft.pat

Examples of com.stevesoft.pat.Regex


//    }

    public IParser create(final IContext ctx) {
        return new ParserBase(ctx) {
            public Token go() {
                Regex r = reg(dialect());
                if (!r.search(remain()))
                    raiseParseException("bad @assign statement. Correct usage: @assign(myVariable){...}");
                String matched = r.stringMatched();
                step(matched.length());
                String s = r.stringMatched(1);
                s = S.stripBrace(s);
                if (matched.startsWith("\n") || matched.endsWith("\n")) {
                    ctx.getCodeBuilder().addBuilder(new Token.StringToken("\n", ctx));
                    Regex r0 = new Regex("\\n([ \\t\\x0B\\f]*).*");
                    if (r0.search(matched)) {
                        String blank = r0.stringMatched(1);
                        if (blank.length() > 0) {
                            ctx.getCodeBuilder().addBuilder(new Token.StringToken(blank, ctx));
                        }
                    }
                } else {
                    Regex r0 = new Regex("([ \\t\\x0B\\f]*).*");
                    if (r0.search(matched)) {
                        String blank = r0.stringMatched(1);
                        if (blank.length() > 0) {
                            ctx.getCodeBuilder().addBuilder(new Token.StringToken(blank, ctx));
                        }
                    }
                }
View Full Code Here


    }

    public IParser create(IContext c) {
        return new RemoveLeadingLineBreakAndSpacesParser(c) {
            public Token go() {
                Regex r = reg(dialect());
                if (!r.search(remain())) {
                    raiseParseException("Error parsing @expand statement. Correct usage: @expand(\"my-macro\")");
                }
                final int curLine = ctx().currentLine();
                step(r.stringMatched().length());
                String s = r.stringMatched(3);
                if (S.isEmpty(s)) {
                    raiseParseException("Error parsing @expand statement. Correct usage: @expand(\"my-macro\")");
                }
                final String macro = S.stripBraceAndQuotation(s);
                return new ExecMacroToken(macro, ctx(), curLine);
View Full Code Here

        return Keyword.EXPAND;
    }

    public static void main(String[] args) {
        ExpandParser p = new ExpandParser();
        Regex r = p.reg(Rythm.INSTANCE);
        String s = "@expand(\"x.y.z\") \n@sayHi(\"green\")";
        if (r.search(s)) {
            p(r);
        }
    }
View Full Code Here

        return new ParserBase(ctx) {
            /*
             * parse @args {...}
             */
            public Token go2(String s) {
                Regex r = reg(dialect());
                final List<F.T4<Integer, String, String, String>> ral = new ArrayList();
                s = s.replaceAll("[\\n\\r]+", ",").replaceAll("[,]+", ",");
                int line = ctx.currentLine();
                while (r.search(s)) {
                    String type = r.stringMatched(2);
                    checkRestrictedClass(type);
                    String name = r.stringMatched(4);
                    String neu = r.stringMatched(6); // check the 'new' keyword
                    if (null == neu) neu = "";
                    String defVal = r.stringMatched(7);
                    if (null != defVal) defVal = neu + " " + defVal;
                    name = ExpressionParser.processPositionPlaceHolder(name);
                    ral.add(new F.T4(line, type, name, defVal));
                }
                return new Directive("", ctx()) {
                    @Override
                    public void call() {
                        for (F.T4<Integer, String, String, String> rd : ral) {
                            builder().addRenderArgs(rd._1, rd._2, rd._3, rd._4);
                        }
                    }
                };
            }

            /*
             * parse @args String s...
             */
            public Token go() {
                String remain = remain();
                Regex r = new Regex(String.format("\\n?[ \\t\\x0B\\f]*%s%s(\\([ \t\f]*\\))?[ \t\f]*((?@{}))\\n?", a(), keyword()));
                if (r.search(remain)) {
                    String matched = r.stringMatched();
                    if (matched.startsWith("\n") || matched.endsWith("\n")) {
                        ctx.getCodeBuilder().addBuilder(new Token.StringToken("\n", ctx));
                    }
                    String s = r.stringMatched(2);
                    s = S.strip(s, "{", "}");
                    step(matched.length());
                    return go2(s);
                }
                boolean startWithLineBreak = remain.startsWith("\n");
                if (startWithLineBreak) {
                    remain = remain.substring(1);
                }
                String space = "";
                Regex r0 = new Regex("^(\\s+).*");
                if (r0.search(remain)) {
                    space = r0.stringMatched(1);
                }
                //space = startWithLineBreak ? "\n" + space : space;
                step(space.length());
                remain = remain.replaceFirst("^\\s+", "");
                String key = String.format("%s%s ", a(), keyword());
View Full Code Here

        };
    }

    public static List<CodeBuilder.RenderArgDeclaration> parseArgDeclaration(int lineNo, String s) {
        final List<CodeBuilder.RenderArgDeclaration> ral = new ArrayList<CodeBuilder.RenderArgDeclaration>();
        Regex r = new ArgsParser().reg(DialectManager.current());
        while (r.search(s)) {
            String matched = r.stringMatched();
            if (matched.startsWith("\n") || matched.startsWith("\r")) {
                break;
            }
            String name = r.stringMatched(4);
            String type = r.stringMatched(2);
            String defVal = r.stringMatched(5);
            name = ExpressionParser.processPositionPlaceHolder(name);
            ral.add(new CodeBuilder.RenderArgDeclaration(lineNo, type, name, defVal));
        }
        return ral;
    }
View Full Code Here

    }

    public IParser create(IContext c) {
        return new RemoveLeadingLineBreakAndSpacesParser(c) {
            public Token go() {
                Regex r = reg(dialect());
                if (r.search(remain())) {
                    step(r.stringMatched().length());
                    String condition = r.stringMatched(2);
                    if (null != condition) {
                        condition = S.stripBrace(condition);
                    }
                    IContext.Continue c = ctx().peekContinue();
                    if (null == c) raiseParseException("Bad @continue statement: No loop context");
View Full Code Here

    }

    public IParser create(IContext ctx) {
        return new RemoveLeadingLineBreakAndSpacesParser(ctx) {
            public Token go() {
                Regex r = reg(dialect());
                if (r.search(remain())) {
                    String s = r.stringMatched();
                    step(s.length());
                }
                return new CodeToken("", ctx());
            }
        };
View Full Code Here

        return new ParserBase(ctx) {

            @Override
            public Token go() {
                final String caret_ = dialect.a();
                final Regex r1 = new Regex(String.format(patternStr1(), caret_));
                final Regex r2 = new Regex(String.format(patternStr2(), caret_));
                final Regex r3 = new Regex(patternStr3());
                final Regex r4 = new Regex(patternStr4());

                String s = remain();
                String exp;
                int step = 0;
                if (r1.search(s)) {
                    exp = r1.stringMatched(2);
                    step = r1.stringMatched(1).length();
                } else if (r2.search(s)) {
                    exp = r2.stringMatched(2);
                    exp = S.stripBrace(exp);
                    step = r2.stringMatched().length();
                } else {
                    return null;
                }
                exp = exp.trim();
                if (!exp.contains("?")) return null; // leave it to normal expression handler
                if (!r4.search(exp)) {
                    //raiseParseException("nullable expression can contain only expression, \"[]\", \"()\", \".\", \"?\", found: %s", exp);
                    return null; // the foo == null ? "bar" : foo style expression?
                }
                step(step);
                StringBuilder curExp = new StringBuilder();
View Full Code Here

    }

    public IParser create(final IContext ctx) {
        return new RemoveLeadingLineBreakAndSpacesParser(ctx) {
            public Token go() {
                Regex r = reg(dialect());
                if (!r.search(remain())) {
                    raiseParseException("Error parsing @exec statement. Correct usage: @exec(myMacro)");
                }
                final int curLine = ctx().currentLine();
                final String matched = r.stringMatched();
                step(matched.length());
                if (matched.startsWith("\n") || matched.endsWith("\n")) {
                    ctx.getCodeBuilder().addBuilder(new Token.StringToken("\n", ctx));
                    Regex r0 = new Regex("\\n([ \\t\\x0B\\f]*).*");
                    if (r0.search(matched)) {
                        String blank = r0.stringMatched(1);
                        if (blank.length() > 0) {
                            ctx.getCodeBuilder().addBuilder(new Token.StringToken(blank, ctx));
                        }
                    }
                } else {
                    Regex r0 = new Regex("([ \\t\\x0B\\f]*).*");
                    if (r0.search(matched)) {
                        String blank = r0.stringMatched(1);
                        if (blank.length() > 0) {
                            ctx.getCodeBuilder().addBuilder(new Token.StringToken(blank, ctx));
                        }
                    }
                }
View Full Code Here

        static void parseParams(String line, ParameterDeclarationList params, IContext ctx) {
            if (S.isEmpty(line)) return;
            // strip '(' and ')'
            line = line.trim();
            if (line.startsWith("(")) line = S.stripBrace(line);
            Regex r = new Regex("\\G(\\s*,\\s*)?((([a-zA-Z_][\\w$_]*)\\s*[=:]\\s*)?((?@())|'.'|(?@\"\")|[0-9\\.]+[l]?|[a-zA-Z_][a-zA-Z0-9_\\.]*(?@())*(?@[])*(?@())*(\\.[a-zA-Z][a-zA-Z0-9_\\.]*(?@())*(?@[])*(?@())*)*@?)|[_a-zA-Z][a-z_A-Z0-9]*@?)");
            line = line.replaceAll("^\\s+", ""); // allow line breaks in params
            line = S.strip(line, "{", "}");
            line = line.replaceAll("^\\s+", ""); // allow line breaks in params
            while (r.search(line)) {
                params.addParameterDeclaration(r.stringMatched(4), r.stringMatched(5), ctx);
            }
        }
View Full Code Here

TOP

Related Classes of com.stevesoft.pat.Regex

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.