Package fasp.parser.parsers

Source Code of fasp.parser.parsers.LiteralParser

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package fasp.parser.parsers;

import fasp.parser.*;
import fasp.parser.tokenizer.Token;
import fasp.parser.tokenizer.TokenState;
import fasp.datatypes.NonGroundPredicate;
import fasp.datatypes.NonGroundLiteral;
import fasp.parser.tokenizer.SymbolToken;
import java.util.HashMap;
import java.util.Map;
import operators.N;
import operators.NM;
import operators.UnaryOperator;

/**
*
* @author Jeroen Janssen <Jeroen.Janssen@vub.ac.be>
*/
public class LiteralParser extends AbstractParser<NonGroundLiteral> {
  private static final Map<String,UnaryOperator> NEGATIONOPERATORS;
  static {
    Map<String,UnaryOperator> myMap = new HashMap<String,UnaryOperator>();
    myMap.put("NL",new N());
    myMap.put("NM",new NM());
    NEGATIONOPERATORS = myMap;
  }

  public NonGroundLiteral parse(TokenState st) throws ParseException {
    TokenParser nafSym = new TokenParser(Token.Type.NAF);
    TokenParser negSym = new TokenParser(Token.Type.NEG);
    boolean naf = false;
    boolean neg = false;
    UnaryOperator negOp = null;
    if(nafSym.tryParse(st)) {
      naf = true;
      Token negOpTok = st.getNext();
      if(!negOpTok.getType().equals(Token.Type.SYMBOL))
        throw new ParseException(negOpTok.getLocation(),"naf operator",negOpTok.toString());

      SymbolToken negOpSym = (SymbolToken)negOpTok;

      if(!NEGATIONOPERATORS.containsKey(negOpSym.getSymbol()))
        throw new ParseException(st,unsupportedNegationOp(),negOpSym.getSymbol());

      negOp = NEGATIONOPERATORS.get(negOpSym.getSymbol());
    }
    if(negSym.tryParse(st))
      neg = true;
    try {
      NonGroundPredicate p = new PredicateParser().parse(st);
      return new NonGroundLiteral(p,neg,naf,negOp);
    } catch (ParseException e) {
      throw new ParseException(st,"failed to parse literal: "+e.getMessage());
    }
  }

  private static String unsupportedNegationOp() {
    return "Bad Negation Operator";
  }
}
TOP

Related Classes of fasp.parser.parsers.LiteralParser

TOP
Copyright © 2018 www.massapi.com. 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.