use of org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken in project jphp by jphp-compiler.
the class ASMExpression method processToken.
protected void processToken(Token token, Stack<Token> stack, List<Token> result) {
if (token instanceof CallExprToken) {
CallExprToken call = (CallExprToken) token;
if (call.getName() instanceof OperatorExprToken) {
processOperator(stack, result, (OperatorExprToken) call.getName());
}
result.add(token);
} else if (token instanceof ValueExprToken) {
result.add(token);
} else if (token instanceof BraceExprToken) {
BraceExprToken brace = (BraceExprToken) token;
if (brace.isSimpleOpened()) {
stack.push(brace);
} else if (brace.isSimpleClosed()) {
if (stack.empty())
unexpectedToken(brace);
boolean done = false;
do {
Token el = stack.pop();
if (el instanceof BraceExprToken && ((BraceExprToken) el).isSimpleOpened()) {
done = true;
break;
}
result.add(el);
} while (!stack.isEmpty());
if (!done)
unexpectedToken(brace);
} else
unexpectedToken(brace);
} else if (token instanceof OperatorExprToken) {
OperatorExprToken operator = (OperatorExprToken) token;
/*boolean done = !stack.empty();
if (done){
if (operator.isRightSide())
done = getPriority(stack.peek()) > prior;
else
done = getPriority(stack.peek()) > prior;
}
if (done){
if (prior == 1){
processOperator(stack, result, prior);
result.add(token);
return;
}
stack.push(token);
return;
}*/
processOperator(stack, result, operator);
stack.push(token);
}
}
use of org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken in project jphp by jphp-compiler.
the class ClassGenerator method processBody.
@SuppressWarnings("unchecked")
protected void processBody(ClassStmtToken result, ListIterator<Token> iterator) {
analyzer.setClazz(result);
Token token = nextToken(iterator);
if (token instanceof BraceExprToken) {
BraceExprToken brace = (BraceExprToken) token;
if (brace.isBlockOpened()) {
List<ConstStmtToken> constants = new ArrayList<ConstStmtToken>();
List<MethodStmtToken> methods = new ArrayList<MethodStmtToken>();
List<ClassVarStmtToken> properties = new ArrayList<ClassVarStmtToken>();
List<Token> modifiers = new ArrayList<Token>();
CommentToken lastComment = null;
boolean breakByClose = false;
while (iterator.hasNext()) {
Token current = iterator.next();
if (current instanceof ExprStmtToken)
unexpectedToken(current, "expression");
if (current instanceof ConstStmtToken) {
if (result.isTrait()) {
unexpectedToken(current);
}
ConstStmtToken one = analyzer.generator(ConstGenerator.class).getToken(current, iterator);
one.setClazz(result);
one.setDocComment(lastComment);
one.setModifier(Modifier.PUBLIC);
for (Token modifier : modifiers) {
if (modifier instanceof PrivateStmtToken) {
one.setModifier(Modifier.PRIVATE);
} else if (modifier instanceof ProtectedStmtToken) {
one.setModifier(Modifier.PROTECTED);
} else if (modifier instanceof PublicStmtToken) {
one.setModifier(Modifier.PUBLIC);
} else {
unexpectedToken(modifier);
}
}
lastComment = null;
constants.add(one);
modifiers.clear();
} else if (isTokenClass(current, ClassGenerator.modifiers)) {
for (Token modifier : modifiers) {
if (modifier.getType() == current.getType())
unexpectedToken(current);
}
modifiers.add(current);
} else if (current instanceof VariableExprToken) {
if (result.isInterface()) {
analyzer.getEnvironment().error(result.toTraceInfo(analyzer.getContext()), ErrorType.E_ERROR, Messages.ERR_INTERFACE_MAY_NOT_INCLUDE_VARS);
}
for (Token modifier : modifiers) {
if (isTokenClass(modifier, FinalStmtToken.class, AbstractStmtToken.class)) {
unexpectedToken(modifier);
}
}
List<ClassVarStmtToken> vars = processProperty(result, (VariableExprToken) current, modifiers, iterator);
if (lastComment != null) {
for (ClassVarStmtToken var : vars) {
var.setDocComment(lastComment);
}
lastComment = null;
}
properties.addAll(vars);
modifiers.clear();
} else if (current instanceof FunctionStmtToken) {
FunctionStmtToken function = analyzer.generator(FunctionGenerator.class).getToken(current, iterator);
if (function == null) {
nextToken(iterator);
unexpectedToken(iterator);
}
MethodStmtToken method = new MethodStmtToken(function);
method.setClazz(result);
method.setDocComment(lastComment);
lastComment = null;
for (Token modifier : modifiers) {
if (modifier instanceof AbstractStmtToken)
method.setAbstract(true);
else if (modifier instanceof StaticExprToken)
method.setStatic(true);
else if (modifier instanceof FinalStmtToken) {
method.setFinal(true);
} else if (modifier instanceof PublicStmtToken) {
if (method.getModifier() != null)
unexpectedToken(modifier);
method.setModifier(Modifier.PUBLIC);
} else if (modifier instanceof PrivateStmtToken) {
if (method.getModifier() != null)
unexpectedToken(modifier);
method.setModifier(Modifier.PRIVATE);
} else if (modifier instanceof ProtectedStmtToken) {
if (method.getModifier() != null)
unexpectedToken(modifier);
method.setModifier(Modifier.PROTECTED);
}
}
if (method.getModifier() == null)
method.setModifier(Modifier.PUBLIC);
methods.add(method);
modifiers.clear();
} else if (current instanceof NamespaceUseStmtToken) {
processUse(result, iterator);
lastComment = null;
} else if (isClosedBrace(current, BraceExprToken.Kind.BLOCK)) {
breakByClose = true;
break;
} else if (current instanceof CommentToken) {
lastComment = (CommentToken) current;
} else
unexpectedToken(current);
}
if (!breakByClose) {
// bug-fix from DN.
token = nextTokenAndPrev(iterator);
if (!isClosedBrace(token, BraceExprToken.Kind.BLOCK)) {
unexpectedToken(token);
}
}
// ---
result.setConstants(constants);
result.setMethods(methods);
result.setProperties(properties);
analyzer.setClazz(null);
return;
}
}
unexpectedToken(token, "{");
}
use of org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken in project jphp by jphp-compiler.
the class FunctionGenerator method getToken.
@SuppressWarnings("unchecked")
public FunctionStmtToken getToken(Token current, ListIterator<Token> iterator, boolean closureAllowed) {
if (current instanceof FunctionStmtToken) {
CommentToken commentToken = null;
iterator.previous();
if (iterator.hasPrevious()) {
int cnt = 0;
while (iterator.hasPrevious()) {
cnt++;
Token previous = iterator.previous();
if (previous.isNamedToken())
continue;
if (previous instanceof CommentToken && ((CommentToken) previous).getKind() == CommentToken.Kind.DOCTYPE) {
commentToken = ((CommentToken) previous);
}
break;
}
for (int i = 0; i < cnt; i++) {
iterator.next();
}
}
iterator.next();
FunctionStmtToken result = (FunctionStmtToken) current;
result.setStatic(analyzer.getFunction() == null);
Class<? extends Token>[] excludes = new Class[] { EchoStmtToken.class, ImportExprToken.class };
if (analyzer.getClazz() != null) {
excludes = new Class[0];
}
Token next = nextTokenSensitive(iterator, excludes);
if (next instanceof AmpersandRefToken) {
result.setReturnReference(true);
next = nextTokenSensitive(iterator, excludes);
}
if (next instanceof NameToken) {
/*if (analyzer.getFunction() != null)
unexpectedToken(current);*/
analyzer.addScope(true);
FunctionStmtToken oldFunction = analyzer.getFunction();
analyzer.setFunction(result);
BraceExprToken brace = nextAndExpected(iterator, BraceExprToken.class);
if (!brace.isSimpleOpened())
unexpectedToken(brace, "(");
result.setNamespace(analyzer.getNamespace());
result.setName((NameToken) next);
result.setDocComment(commentToken);
processArguments(result, iterator);
processBody(result, iterator);
result.setTypeInfo(analyzer.getScope().getTypeInfo());
result.setLabels(analyzer.getScope().getLabels());
result.setLocal(analyzer.removeScope().getVariables());
Token previous = iterator.previous();
result.getMeta().setEndLine(previous.getMeta().getStartLine());
result.getMeta().setEndPosition(previous.getMeta().getStartPosition());
iterator.next();
analyzer.setFunction(oldFunction);
return result;
} else if (next instanceof BraceExprToken) {
// xClosure
if (((BraceExprToken) next).isSimpleOpened()) {
if (closureAllowed) {
analyzer.pushClosure(result);
analyzer.addScope(true);
processArguments(result, iterator);
processUses(result, iterator);
processBody(result, iterator);
// boolean thisExists = result.isThisExists();
result.setTypeInfo(analyzer.getScope().getTypeInfo());
result.setLabels(analyzer.getScope().getLabels());
result.setStaticExists(analyzer.getScope().isStaticExists());
result.setLocal(analyzer.removeScope().getVariables());
// result.setThisExists(thisExists);
analyzer.popClosure();
FunctionStmtToken prevClosure = analyzer.peekClosure();
if (prevClosure != null) {
if (result.isThisExists()) {
analyzer.getScope().addVariable(FunctionStmtToken.thisVariable);
// prevClosure.variable(FunctionStmtToken.thisVariable).setUsed(true);
// prevClosure.setThisExists(true);
}
}
List<VariableExprToken> uses = new ArrayList<VariableExprToken>();
for (ArgumentStmtToken argument : result.getUses()) {
if (argument.isReference()) {
if (analyzer.getFunction() != null) {
analyzer.getFunction().variable(argument.getName()).setReference(true);
}
}
if (analyzer.getFunction() != null) {
analyzer.getFunction().variable(argument.getName()).setUsed(true);
}
uses.add(argument.getName());
}
analyzer.getScope().addVariables(uses);
Token previous = iterator.previous();
result.getMeta().setEndLine(previous.getMeta().getStartLine());
result.getMeta().setEndPosition(previous.getMeta().getStartPosition());
iterator.next();
return result;
}
iterator.previous();
return null;
}
}
unexpectedToken(next);
}
return null;
}
use of org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken in project jphp by jphp-compiler.
the class FunctionGenerator method processArgument.
@SuppressWarnings("unchecked")
protected ArgumentStmtToken processArgument(ListIterator<Token> iterator) {
boolean isReference = false;
boolean isVariadic = false;
VariableExprToken variable = null;
ExprStmtToken value = null;
Token next = nextToken(iterator);
if (next instanceof CommaToken || isClosedBrace(next, BraceExprToken.Kind.SIMPLE))
return null;
NameToken hintTypeClass = null;
HintType hintType = null;
boolean optional = false;
if (next instanceof ValueIfElseToken) {
optional = true;
next = nextToken(iterator);
}
if (next instanceof SelfExprToken) {
if (analyzer.getClazz() == null) {
unexpectedToken(next);
}
if (!analyzer.getClazz().isTrait()) {
next = analyzer.getClazz().getName();
hintTypeClass = analyzer.getRealName((NameToken) next);
} else {
hintType = HintType.SELF;
}
next = nextToken(iterator);
} else if (next instanceof NameToken) {
String word = ((NameToken) next).getName().toLowerCase();
if (scalarTypeHints.contains(word)) {
hintType = HintType.of(word);
} else if (disallowScalarTypeHints.contains(word)) {
unexpectedToken(next, "valid type");
} else {
hintType = jphp_scalarTypeHints.contains(word) ? null : HintType.of(word);
if (hintType == null)
hintTypeClass = analyzer.getRealName((NameToken) next);
}
next = nextToken(iterator);
}
if (next instanceof AmpersandRefToken) {
isReference = true;
next = nextToken(iterator);
}
if (next instanceof ArgumentUnpackExprToken) {
isVariadic = true;
next = nextToken(iterator);
}
if (next instanceof VariableExprToken) {
variable = (VariableExprToken) next;
} else
unexpectedToken(next);
next = nextToken(iterator);
if (next instanceof AssignExprToken) {
if (isVariadic) {
unexpectedToken(next);
}
value = analyzer.generator(SimpleExprGenerator.class).getToken(nextToken(iterator), iterator, true, BraceExprToken.Kind.SIMPLE);
} else {
if (next instanceof CommaToken || isClosedBrace(next, BraceExprToken.Kind.SIMPLE)) {
if (next instanceof BraceExprToken) {
iterator.previous();
} else {
if (isVariadic) {
unexpectedToken(next);
}
}
} else
unexpectedToken(next);
}
ArgumentStmtToken argument = new ArgumentStmtToken(variable.getMeta());
argument.setName(variable);
argument.setHintType(hintType);
argument.setHintTypeClass(hintTypeClass);
argument.setReference(isReference);
argument.setVariadic(isVariadic);
argument.setValue(value);
argument.setOptional(optional);
if (argument.isReference() && argument.getValue() != null)
analyzer.getFunction().variable(argument.getName()).setUsed(true);
return argument;
}
use of org.develnext.jphp.core.tokenizer.token.expr.BraceExprToken in project jphp by jphp-compiler.
the class ASMExpression method processOperator.
protected void processOperator(Stack<Token> stack, List<Token> result, OperatorExprToken current) {
List<Token> list = new ArrayList<Token>();
boolean isRightOperator = current != null && current.isRightSide();
int prior = current == null ? -1 : current.getPriority();
while (!stack.empty()) {
Token el = stack.peek();
int elPrior = getPriority(el);
if (el instanceof BraceExprToken)
break;
if (current != null && current.getAssociation() == Association.RIGHT && !current.isBinary() && prev instanceof OperatorExprToken)
break;
boolean flush = current == null || elPrior == 1 || (isRightOperator ? elPrior > prior : elPrior <= prior);
if (flush) {
stack.pop();
list.add(el);
} else {
break;
}
}
result.addAll(list);
}
Aggregations