use of org.develnext.jphp.core.tokenizer.token.CommentToken in project jphp by jphp-compiler.
the class SyntaxAnalyzer method process.
protected void process() {
tokenizer.reset();
tree.clear();
Token token;
while ((token = tokenizer.nextToken()) != null) {
if (token instanceof CommentToken) {
if (((CommentToken) token).getKind() != CommentToken.Kind.DOCTYPE)
continue;
}
tokens.add(token);
}
/*if (tokenizer.hasDirective("mode")){
Directive mode = tokenizer.getDirective("mode");
try {
this.langMode = LangMode.valueOf(mode.value.toUpperCase());
} catch (IllegalArgumentException e){
environment.warning(
mode.trace, "Invalid value '%s' for directive 'mode'", mode.value
);
}
}*/
ListIterator<Token> iterator = tokens.listIterator();
tree = process(iterator);
}
use of org.develnext.jphp.core.tokenizer.token.CommentToken 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 (!modifiers.isEmpty())
unexpectedToken(modifiers.get(0));
if (result.isTrait())
unexpectedToken(current);
ConstStmtToken one = analyzer.generator(ConstGenerator.class).getToken(current, iterator);
one.setClazz(result);
one.setDocComment(lastComment);
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.CommentToken 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.CommentToken in project jphp by jphp-compiler.
the class Generator method nextToken.
protected Token nextToken(ListIterator<Token> iterator) {
checkUnexpectedEnd(iterator);
Token tk = iterator.next();
if (tk instanceof CommentToken)
return nextToken(iterator);
return tk;
}
use of org.develnext.jphp.core.tokenizer.token.CommentToken in project jphp by jphp-compiler.
the class Generator method nextTokenAndPrev.
protected Token nextTokenAndPrev(ListIterator<Token> iterator) {
checkUnexpectedEnd(iterator);
Token result = iterator.next();
if (result instanceof CommentToken) {
return nextTokenAndPrev(iterator);
}
iterator.previous();
return result;
}
Aggregations