📄 exprfactory.java
字号:
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * * Free Software Foundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 USA * * @author Scott Ferguson */package com.caucho.quercus.expr;import com.caucho.quercus.Location;import com.caucho.quercus.Quercus;import com.caucho.quercus.env.*;import com.caucho.quercus.parser.QuercusParser;import com.caucho.quercus.program.*;import com.caucho.util.L10N;import com.caucho.vfs.Path;import java.util.ArrayList;import java.util.logging.Level;import java.util.logging.Logger;/** * Factory for creating PHP expressions and statements */public class ExprFactory { private static final L10N L = new L10N(ExprFactory.class); private static final Logger log = Logger.getLogger(ExprFactory.class.getName()); public ExprFactory() { } public static ExprFactory create() { try { Class cl = Class.forName("com.caucho.quercus.expr.ProExprFactory"); return (ExprFactory) cl.newInstance(); } catch (Exception e) { log.log(Level.FINEST, e.toString(), e); return new ExprFactory(); } } /** * Creates a null literal expression. */ public Expr createNull() { return NullLiteralExpr.NULL; } /** * Creates a string (php5) literal expression. */ public Expr createString(String lexeme) { return new StringLiteralExpr(lexeme); } /** * Creates a string literal expression. */ public Expr createUnicode(String lexeme) { return new UnicodeLiteralExpr(lexeme); } /** * Creates a binary literal expression. */ public Expr createBinary(byte []bytes) { return new BinaryLiteralExpr(bytes); } /** * Creates a binary literal expression. */ public Expr createBinary(String bytes, String encoding) { return new BinaryLiteralExpr(bytes, encoding); } /** * Creates a long literal expression. */ public Expr createLong(long value) { return new LongLiteralExpr(value); } /** * Creates a string literal expression. */ public Expr createLiteral(Value literal) { return new LiteralExpr(literal); } /** * Creates a var expression. */ public VarExpr createVar(VarInfo var) { return new VarExpr(var); } /** * Creates a var expression. */ public VarVarExpr createVarVar(Expr var) { return new VarVarExpr(var); } /** * Creates a const expression. */ public ConstExpr createConst(String name) { return new ConstExpr(name); } /** * Creates a class const expression. */ public ClassConstExpr createClassConst(String className, String name) { return new ClassConstExpr(className, name); } /** * Creates a class const expression (static::FOO). */ public LateStaticBindingClassConstExpr createLateStaticBindingClassConst(String name) { return new LateStaticBindingClassConstExpr(name); } /** * Creates a this expression. */ public ThisExpr createThis(Location location, InterpretedClassDef cl) { return new ThisExpr(location, cl); } /** * Creates an array get 'a[0]' expression. * @param location */ public ArrayGetExpr createArrayGet(Location location, Expr base, Expr index) { return new ArrayGetExpr(location, base, index); } /** * Creates an array tail 'a[]' expression. * @param location TODO */ public ArrayTailExpr createArrayTail(Location location, Expr base) { return new ArrayTailExpr(location, base); } /** * Creates an object get '$a->b' expression. */ public Expr createFieldGet(Location location, Expr base, StringValue name) { return new FieldGetExpr(location, base, name); } /** * Creates an object get '$a->$b' expression. */ public Expr createFieldVarGet(Location location, Expr base, Expr name) { return new FieldVarGetExpr(location, base, name); } /** * Creates an object get 'a::b' expression. */ public Expr createStaticFieldGet(Location location, String className, String name) { return new StaticFieldGetExpr(location, className, name); } /** * Creates an object get 'static::b' expression. */ public Expr createLateStaticBindingFieldGet(Location location, String name) { return new LateStaticBindingFieldGetExpr(location, name); } /** * Creates an object get 'a::${b}' expression. */ public Expr createStaticFieldVarGet(Location location, String className, Expr name) { return new StaticFieldVarGetExpr(location, className, name); } /** * Creates an object get 'static::${b}' expression. */ public Expr createLateStaticBindingFieldVarGet(Location location, Expr name) { return new LateStaticBindingFieldVarGetExpr(location, name); } /** * Creates an unset '$a' expression. */ public Expr createUnsetVar(AbstractVarExpr var) { return new UnsetVarExpr(var); } /** * Creates a char at 'a{0}' expression. */ public CharAtExpr createCharAt(Expr base, Expr index) { return new CharAtExpr(base, index); } /** * Creates a post increment 'a++' expression. */ public PostIncrementExpr createPostIncrement(Expr expr, int incr) { return new PostIncrementExpr(expr, incr); } /** * Creates a pre increment '++a' expression. */ public PreIncrementExpr createPreIncrement(Expr expr, int incr) { return new PreIncrementExpr(expr, incr); } /** * Creates a unary minus '-a' expression. */ public Expr createMinus(Expr expr) { return new MinusExpr(expr); } /** * Creates a unary plus '+a' expression. */ public Expr createPlus(Expr expr) { return new PlusExpr(expr); } /** * Creates a unary not '!a' expression. */ public Expr createNot(Expr expr) { return new NotExpr(expr); } /** * Creates a unary inversion '~a' expression. */ public Expr createBitNot(Expr expr) { return new BitNotExpr(expr); } /** * Creates a clone 'clone a' expression. */ public Expr createClone(Expr expr) { return new CloneExpr(expr); } /** * Creates a clone 'clone a' expression. */ public Expr createCopy(Expr expr) { return new CopyExpr(expr); } /** * Creates an error suppression '@a' expression. */ public Expr createSuppress(Expr expr) { return new SuppressErrorExpr(expr); } /** * Creates a boolean cast */ public Expr createToBoolean(Expr expr) { return new ToBooleanExpr(expr); } /** * Creates a long cast */ public Expr createToLong(Expr expr) { return new ToLongExpr(expr); } /** * Creates a double cast */ public Expr createToDouble(Expr expr) { return new ToDoubleExpr(expr); } /** * Creates a string cast */ public Expr createToString(Expr expr) { return new ToStringExpr(expr); } /** * Creates a unicode cast */ public Expr createToUnicode(Expr expr) { return new ToUnicodeExpr(expr); } /** * Creates a binary string cast */ public Expr createToBinary(Expr expr) { return new ToBinaryExpr(expr); } /** * Creates an object cast */ public Expr createToObject(Expr expr) { return new ToObjectExpr(expr); } /** * Creates an array cast */ public Expr createToArray(Expr expr) { return new ToArrayExpr(expr); } /** * Creates a die 'die("msg")' expression. */ public Expr createDie(Expr expr) { return new DieExpr(expr); } /** * Creates an exit 'exit("msg")' expression. */ public Expr createExit(Expr expr) { return new ExitExpr(expr); } /** * Creates a required */ public Expr createRequired() { return new RequiredExpr(); } /** * Creates a default */ public Expr createDefault() { return new DefaultExpr(); } /** * Creates an addition expression. */ public Expr createAdd(Expr left, Expr right) { return new AddExpr(left, right); } /** * Creates a subtraction expression. */ public Expr createSub(Expr left, Expr right) { return new SubExpr(left, right); } /** * Creates a multiplication expression. */ public Expr createMul(Expr left, Expr right) { return new MulExpr(left, right); } /** * Creates a division expression. */ public Expr createDiv(Expr left, Expr right) { return new DivExpr(left, right); } /** * Creates a modulo expression. */ public Expr createMod(Expr left, Expr right) { return new ModExpr(left, right); } /** * Creates a left-shift expression. */ public Expr createLeftShift(Expr left, Expr right) { return new LeftShiftExpr(left, right); } /** * Creates a right-shift expression. */ public Expr createRightShift(Expr left, Expr right) { return new RightShiftExpr(left, right); } /** * Creates a bit-and expression. */ public Expr createBitAnd(Expr left, Expr right) { return new BitAndExpr(left, right); } /** * Creates a bit-or expression. */ public Expr createBitOr(Expr left, Expr right) { return new BitOrExpr(left, right); } /** * Creates a bit-xor expression. */ public Expr createBitXor(Expr left, Expr right) { return new BitXorExpr(left, right); } /** * Creates an append expression */ public final Expr createAppend(Expr left, Expr right) { AppendExpr leftAppend; // XXX: i18n binary vs unicode issues /* if (left instanceof ToStringExpr) left = ((ToStringExpr) left).getExpr(); if (left instanceof StringLiteralExpr) { StringLiteralExpr string = (StringLiteralExpr) left; if (string.evalConstant().length() == 0) return ToStringExpr.create(right); } */ if (left instanceof AppendExpr) leftAppend = (AppendExpr) left; else leftAppend = createAppendImpl(left, null); AppendExpr next; /* if (right instanceof ToStringExpr) right = ((ToStringExpr) right).getExpr(); if (right instanceof StringLiteralExpr) { StringLiteralExpr string = (StringLiteralExpr) right; if (string.evalConstant().length() == 0) return ToStringExpr.create(left); } */ if (right instanceof AppendExpr) next = (AppendExpr) right; else next = createAppendImpl(right, null); AppendExpr result = append(leftAppend, next); if (result.getNext() != null) return result; else return result.getValue(); } /** * Appends the tail to the current expression, combining * constant literals. */ private AppendExpr append(AppendExpr left, AppendExpr tail) { if (left == null) return tail; tail = append(left.getNext(), tail); if (left.getValue() instanceof BinaryLiteralExpr && tail.getValue() instanceof BinaryLiteralExpr) { BinaryLiteralExpr leftString = (BinaryLiteralExpr) left.getValue(); BinaryLiteralExpr rightString = (BinaryLiteralExpr) tail.getValue(); try { byte []bytes = (leftString.evalConstant().toString() + rightString.evalConstant().toString()).getBytes("ISO-8859-1"); Expr value = createBinary(bytes); return createAppendImpl(value, tail.getNext()); } catch (java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); } } else if (left.getValue() instanceof BinaryLiteralExpr || tail.getValue() instanceof BinaryLiteralExpr) { left.setNext(tail); return left; } else if (left.getValue() instanceof StringLiteralExpr && tail.getValue() instanceof StringLiteralExpr) { StringLiteralExpr leftString = (StringLiteralExpr) left.getValue(); StringLiteralExpr rightString = (StringLiteralExpr) tail.getValue(); Expr value = createString(leftString.evalConstant().toString() + rightString.evalConstant().toString()); return createAppendImpl(value, tail.getNext()); } else if (left.getValue() instanceof UnicodeLiteralExpr && tail.getValue() instanceof UnicodeLiteralExpr) { UnicodeLiteralExpr leftString = (UnicodeLiteralExpr) left.getValue(); UnicodeLiteralExpr rightString = (UnicodeLiteralExpr) tail.getValue(); Expr value = createUnicode(leftString.evalConstant().toString() + rightString.evalConstant().toString()); return createAppendImpl(value, tail.getNext()); } else { left.setNext(tail); return left; } } protected AppendExpr createAppendImpl(Expr left, AppendExpr right) { return new AppendExpr(left, right); } /** * Creates a lt expression. */ public Expr createLt(Expr left, Expr right) { return new LtExpr(left, right); } /** * Creates a leq expression. */ public Expr createLeq(Expr left, Expr right) { return new LeqExpr(left, right); } /** * Creates a gt expression. */ public Expr createGt(Expr left, Expr right) { return new GtExpr(left, right); } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -