All Downloads are FREE. Search and download functionalities are using the official Maven repository.

zoomba.lang.parser.ASTPatternLiteral Maven / Gradle / Ivy

/* Generated By:JJTree: Do not edit this line. ASTPatternLiteral.java Version 4.3 */
/* JavaCCOptions:MULTI=true,NODE_USES_PARSER=false,VISITOR=true,TRACK_TOKENS=true,NODE_PREFIX=AST,NODE_EXTENDS=,NODE_FACTORY=,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */
package zoomba.lang.parser;

import zoomba.lang.core.interpreter.ZInterpret;

import java.util.regex.Pattern;

public class ASTPatternLiteral extends ZoombaNode {

    String expression;

    int flags;

    Pattern pattern;

    public Pattern pattern(){ return pattern ; }

    boolean anywhere;

    public boolean anywhere(){ return anywhere ; }

    public ASTPatternLiteral(int id) {
        super(id);
    }

    public ASTPatternLiteral(Parser p, int id) {
        super(p, id);
    }

    /** Accept the visitor. **/
    public Object jjtAccept(ParserVisitor visitor, Object data) {
        return visitor.visit(this, data);
    }


    /**
     * Always lazy evaluation
     * @param s the string
     * @param i interpreter
     * @return true if string matches, false if does not
     */
    public boolean matches(String s, ZInterpret i){
        Pattern p;
        if ( pattern != null ){
            p = pattern;
        } else {
            String expr = String.valueOf(i.execInline(expression));
            p = Pattern.compile(expr,flags);
        }

        if (anywhere) {
            return p.matcher(s).find();
        } else {
            return p.matcher(s).matches();
        }
    }

    /***
     * ##xyz#globals
     * @param s the pattern string
     */
    public void setPattern(String s){
        String sPat = s.substring(2);
        int endIndex = sPat.lastIndexOf('#');
        String modifiers = sPat.substring(endIndex+1);
        sPat  = sPat.substring(0,endIndex);
        anywhere = false ;
        flags = Pattern.DOTALL ;
        if ( modifiers.contains("i") ){
            flags = flags | Pattern.CASE_INSENSITIVE ;
        }
        if ( modifiers.contains("m") ){
            flags = flags | Pattern.MULTILINE ;
        }
        if ( modifiers.contains("g")){
            anywhere = true ;
        }
        if ( sPat.startsWith("?")){
            pattern = null ;
            expression = sPat.substring(1);
        } else {
            pattern = Pattern.compile(sPat, flags);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy