use of org.develnext.jphp.core.tokenizer.token.Token in project jphp by jphp-compiler.
the class SyntaxAnalyzer method processNext.
public Token processNext(ListIterator<Token> iterator) {
Token current = iterator.next();
Token gen = generateToken(current, iterator);
return (gen == null ? current : gen);
}
use of org.develnext.jphp.core.tokenizer.token.Token in project jphp by jphp-compiler.
the class ClassGenerator method processUse.
protected void processUse(ClassStmtToken result, ListIterator<Token> iterator) {
if (result.isInterface())
unexpectedToken(iterator.previous());
Token prev = null;
List<NameToken> uses = new ArrayList<NameToken>();
do {
Token token = nextToken(iterator);
if (token instanceof NameToken) {
uses.add(analyzer.getRealName((NameToken) token));
} else if (token instanceof CommaToken) {
if (!(prev instanceof NameToken))
unexpectedToken(token);
} else if (token instanceof SemicolonToken) {
break;
} else if (isOpenedBrace(token, BraceExprToken.Kind.BLOCK)) {
processBlock(result, uses.get(0), iterator);
break;
} else
unexpectedToken(token);
prev = token;
} while (true);
result.getUses().addAll(uses);
}
use of org.develnext.jphp.core.tokenizer.token.Token in project jphp by jphp-compiler.
the class ClassGenerator method processExtends.
protected void processExtends(ClassStmtToken result, ListIterator<Token> iterator) {
Token token = nextToken(iterator);
if (token instanceof ExtendsStmtToken) {
if (result.isTrait())
unexpectedToken(token);
if (result.isInterface()) {
_processImplements(token, result, iterator);
} else {
ExtendsStmtToken extend = (ExtendsStmtToken) token;
FulledNameToken what = analyzer.generator(NameGenerator.class).getToken(nextToken(iterator), iterator);
if (what == null) {
unexpectedToken(extend);
}
extend.setName(analyzer.getRealName(what));
result.setExtend(extend);
}
} else
iterator.previous();
}
use of org.develnext.jphp.core.tokenizer.token.Token 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;
ValueExprToken type = null;
Boolean nullable = 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, type, nullable != null && nullable, iterator);
if (lastComment != null) {
for (ClassVarStmtToken var : vars) {
var.setDocComment(lastComment);
}
lastComment = null;
}
properties.addAll(vars);
modifiers.clear();
type = null;
} 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 if ((current instanceof NameToken || current instanceof SelfExprToken) && type == null) {
type = (ValueExprToken) current;
} else if (current instanceof ValueIfElseToken) {
nullable = true;
Token nxt = nextTokenAndPrev(iterator);
if (!nxt.isNamedToken()) {
unexpectedToken(nxt);
}
} 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.Token in project jphp by jphp-compiler.
the class ClassGenerator method processProperty.
protected List<ClassVarStmtToken> processProperty(ClassStmtToken clazz, VariableExprToken current, List<Token> modifiers, ValueExprToken type, boolean nullable, ListIterator<Token> iterator) {
Token next = current;
Token prev = null;
Set<VariableExprToken> variables = new LinkedHashSet<VariableExprToken>();
List<ExprStmtToken> initValues = new ArrayList<>();
NameToken hintTypeClass = null;
HintType hintType = null;
if (type != null) {
if (type instanceof NameToken) {
String word = ((NameToken) type).getName().toLowerCase();
/*if (disallowScalarTypesForProps.contains(word)) {
unexpectedToken(next, "valid type");
} else */
if (FunctionGenerator.scalarTypeHints.contains(word)) {
hintType = HintType.of(word);
} else {
hintType = FunctionGenerator.jphp_scalarTypeHints.contains(word) ? null : HintType.of(word);
if (hintType == null)
hintTypeClass = analyzer.getRealName((NameToken) type);
}
}
}
ExprStmtToken initValue = null;
List<ClassVarStmtToken> result = new ArrayList<ClassVarStmtToken>();
Modifier modifier = Modifier.PUBLIC;
boolean isStatic = false;
for (Token token : modifiers) {
if (token instanceof PrivateStmtToken)
modifier = Modifier.PRIVATE;
else if (token instanceof ProtectedStmtToken)
modifier = Modifier.PROTECTED;
else if (token instanceof StaticExprToken)
isStatic = true;
}
do {
if (next instanceof VariableExprToken) {
if (!variables.add((VariableExprToken) next)) {
throw new ParseException(Messages.ERR_IDENTIFIER_X_ALREADY_USED.fetch(next.getWord()), next.toTraceInfo(analyzer.getContext()));
}
initValues.add(null);
} else if (next instanceof CommaToken) {
if (!(prev instanceof VariableExprToken))
unexpectedToken(next);
} else if (next instanceof AssignExprToken) {
if (!(prev instanceof VariableExprToken))
unexpectedToken(next);
initValue = analyzer.generator(SimpleExprGenerator.class).getToken(nextToken(iterator), iterator, Separator.COMMA_OR_SEMICOLON, null);
initValues.set(initValues.size() - 1, initValue);
if (iterator.hasPrevious() && isBreak(iterator.previous())) {
iterator.next();
break;
}
if (iterator.hasNext()) {
iterator.next();
}
// break;
} else if (next instanceof SemicolonToken) {
if (!(prev instanceof VariableExprToken))
unexpectedToken(next);
break;
} else {
unexpectedToken(next);
}
prev = next;
next = nextToken(iterator);
} while (true);
int i = 0;
for (VariableExprToken variable : variables) {
ClassVarStmtToken classVar = new ClassVarStmtToken(variable.getMeta());
classVar.setModifier(modifier);
classVar.setStatic(isStatic);
classVar.setValue(initValues.get(i));
classVar.setVariable(variable);
classVar.setClazz(clazz);
classVar.setHintType(hintType);
classVar.setHintTypeClass(hintTypeClass);
classVar.setNullable(nullable);
result.add(classVar);
i++;
}
return result;
}
Aggregations