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

net.sf.okapi.core.simplifierrules.SimplifierRules.jj Maven / Gradle / Ivy

/*
  Okapi Simplifier Rules Parser
  Copyright (C) 2015 by the Okapi Framework contributors
  -----------------------------------------------------------------------------
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

*/

options
{
  LOOKAHEAD=2;
  FORCE_LA_CHECK=false;
  GENERATE_ANNOTATIONS=true; 
  GENERATE_CHAINED_EXCEPTION=true; 
  SUPPORT_CLASS_VISIBILITY_PUBLIC=true; 
  GENERATE_GENERICS=true; 
  GENERATE_STRING_BUILDER=true; 
  UNICODE_INPUT=true; 
  JAVA_UNICODE_ESCAPE=true; 
  ERROR_REPORTING = true;
  IGNORE_CASE = false;
  SANITY_CHECK = true;
  STATIC = false;
  KEEP_LINE_COLUMN = true;
  JDK_VERSION="1.8";
}

PARSER_BEGIN(SimplifierRules)
package net.sf.okapi.core.simplifierrules;

import java.io.StringReader;

import net.sf.okapi.common.resource.Code;
import net.sf.okapi.common.resource.TextFragment.TagType;
import net.sf.okapi.common.Util;

public class SimplifierRules
{
  private Code code;
  
  public static void main(String args [])
  {
    System.out.println("Reading from standard input...");
    System.out.print("Enter an expression like \"if ADDABLE;\" :");
    SimplifierRules parser = new SimplifierRules(System.in);
    try
    {
      boolean r = parser.rules();
    }
    catch (Exception e)
    {
      System.out.println("Oops.");
      System.out.println(e.getMessage());
    }
  }
  
  public final static void validate(String rules) throws ParseException {
    SimplifierRules r = new SimplifierRules(rules, new Code());
    r.parse();
  }
  
  public SimplifierRules()
  {   
  }
  
  public SimplifierRules(String input, Code code)
  {
    this(new StringReader(input));
    this.code = code;
  }
  
  public boolean evaluate(String input, Code code) throws ParseException
  {
    if (Util.isEmpty(input) || code == null) {
        return false;
    }
    
    ReInit(new StringReader(input));
    this.code = code;
    return parse();
  }
  
  public boolean parse() throws ParseException
  {
    return rules();
  }
}

PARSER_END(SimplifierRules)

// skip all whitespace and comments
SKIP :
{
  " "
| "\t"
| "\n"
| "\r"
| <"#" (~[ "\n", "\r" ])*
    (
      "\n"
    | "\r"
    | "\r\n"
    )>
| <"/*" (~[ "*" ])* "*"
    (
      ~[ "/" ] (~[ "*" ])* "*"
    )*
    "/">
}

TOKEN :
{
  
|  
}

// Code fields: data, outerData, originalId, type, TagType 
TOKEN :
{
  
|   
| 
| 
| 
}

// literals: CLOSING, OPENING, STANDALONE, addable, deletable or cloneable
TOKEN :
{
  
|   
| 
| 
| 
| 
}

// boolean operators: or, and
TOKEN :
{
  
| 
| 
| 
}

// operators: =, ~, !=, !~
TOKEN :
{
  
| 
}

// String tokens
TOKEN :
{
  <#QUOTE_DOUBLE : "\"">
| 
| <#STRING_DOUBLE_BODY :
    (
      (~[ "\"", "\\", "\r", "\n", "\t" ])
    |
      (
        "\\"
        (
          "r"
        | "n"
        | "\\"
        | "\""
        | "t"
        )
      )
    )+>
|   >
}

/**
    All rules are OR'ed with each other to get the final result.
    Short circuit and return early if any rule evaluates to true    
*/
boolean rules() :
{ boolean result=false; boolean r; }
{
  ( r=expression()  { if (r) return true; result |= r; })+ 
  { return result; }
}

boolean expression() :
{ boolean result; boolean tail; }
{
    result=term() 
    (
        ( tail=term() { result &= tail; } )  
        | 
        ( tail=term() { result |= tail; })
    )*
    { return result; }
}

boolean term() :
{ boolean result; TagType ctt; TagType ltt; String cs; String qs;}
{
  result=flagLiteral() { return result; }
  | 
  (ctt=codeTagTypeField()  ltt=tagTypeLiteral() { return ctt == ltt; }) 
  |
  (ctt=codeTagTypeField()  ltt=tagTypeLiteral() { return ctt != ltt; })
  | 
  (cs=codeString()  qs=queryString() { return cs.equals(qs); } )
  |
  (cs=codeString()  qs=queryString() { return !cs.equals(qs); })
  |
  (cs=codeString()  qs=queryString() { return cs.matches(qs); })
  |
  (cs=codeString()  qs=queryString() { return !cs.matches(qs); })
  | 
  ( result=expression() ) { return result; }
}

String codeString() : 
{}
{
   { return code.getData() == null ? "" : code.getData(); } 
  | 
   { return code.getOuterData() == null ? "" : code.getOuterData(); } 
  | 
   { return code.getOriginalId() == null ? "" : code.getOriginalId(); }
  | 
   { return code.getType() == null ? "" : code.getType(); }
}

TagType codeTagTypeField() :
{}
{
   { return code.getTagType(); } 
}

TagType tagTypeLiteral() :
{}
{
   { return TagType.CLOSING; }
|  { return TagType.OPENING; }  
|  { return TagType.PLACEHOLDER; }
}

boolean flagLiteral() :
{}
{
 ( { return code.isAdded(); }
|  { return code.isDeleteable(); }
|  { return code.isCloneable(); })
}

String queryString() :
{Token t; }
{
(t= 
{ return ""; }  
| 
t=)
// remove quotes and unescape all escaped chars
{ return SimplifierRulesUtil.unescape(t.image.substring(1, t.image.length()-1)); }     
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy