use of org.develnext.jphp.core.tokenizer.token.Token in project jphp by jphp-compiler.
the class ClassGenerator method processBlock.
protected void processBlock(ClassStmtToken result, NameToken firstTraitName, ListIterator<Token> iterator) {
if (result.isInterface())
unexpectedToken(iterator.previous());
while (true) {
NameToken className = nextAndExpectedSensitive(iterator, NameToken.class);
NameToken methodName;
Token token = nextToken(iterator);
if (token instanceof StaticAccessExprToken) {
className = analyzer.getRealName(className);
Token nextTokenSensitive = nextTokenSensitive(iterator);
if (nextTokenSensitive instanceof NameToken) {
methodName = (NameToken) nextTokenSensitive;
} else {
unexpectedToken(nextTokenSensitive);
return;
}
} else {
iterator.previous();
methodName = className;
if (className.getClass() != NameToken.class)
unexpectedToken(className);
className = firstTraitName;
}
Token what = nextToken(iterator);
if (what instanceof AsStmtToken) {
Token one = nextTokenSensitive(iterator, PrivateStmtToken.class, ProtectedStmtToken.class, PublicStmtToken.class, FinalStmtToken.class, StaticExprToken.class);
if (one instanceof NameToken) {
result.addAlias(className.getName(), methodName.getName(), null, ((NameToken) one).getName());
} else if (isTokenClass(one, PrivateStmtToken.class, ProtectedStmtToken.class, PublicStmtToken.class)) {
Modifier modifier = Modifier.PRIVATE;
if (one instanceof ProtectedStmtToken)
modifier = Modifier.PROTECTED;
else if (one instanceof PublicStmtToken)
modifier = Modifier.PUBLIC;
token = nextTokenSensitive(iterator);
String name = null;
if (token instanceof NameToken) {
NameToken two = (NameToken) token;
if (two.getClass() != NameToken.class)
unexpectedToken(two);
name = two.getName();
} else if (token instanceof SemicolonToken) {
iterator.previous();
// nop
} else
unexpectedToken(token);
result.addAlias(className.getName(), methodName.getName(), modifier, name);
} else
unexpectedToken(one);
nextAndExpected(iterator, SemicolonToken.class);
} else if (what instanceof InsteadofStmtToken) {
Set<String> names = new HashSet<String>();
Set<String> namesLower = new HashSet<String>();
NameToken cls;
while (true) {
cls = nextAndExpected(iterator, NameToken.class);
cls = analyzer.getRealName(cls);
if (!namesLower.add(cls.getName().toLowerCase())) {
analyzer.getEnvironment().error(iterator.previous().toTraceInfo(analyzer.getContext()), ErrorType.E_ERROR, Messages.ERR_TRAIT_MULTIPLE_RULE, methodName.getName(), cls.getName());
}
names.add(cls.getName());
Token next = nextToken(iterator);
if (next instanceof CommaToken)
continue;
if (next instanceof SemicolonToken)
break;
}
if (!result.addReplacement(className.getName(), methodName.getName(), names)) {
analyzer.getEnvironment().error(iterator.previous().toTraceInfo(analyzer.getContext()), ErrorType.E_ERROR, Messages.ERR_TRAIT_MULTIPLE_RULE, methodName.getName(), cls.getName());
}
} else
unexpectedToken(what);
if (isClosedBrace(nextTokenAndPrev(iterator), BraceExprToken.Kind.BLOCK)) {
iterator.next();
break;
}
}
}
use of org.develnext.jphp.core.tokenizer.token.Token in project jphp by jphp-compiler.
the class ClassGenerator method _processImplements.
protected void _processImplements(Token token, ClassStmtToken result, ListIterator<Token> iterator) {
ImplementsStmtToken implement = new ImplementsStmtToken(token.getMeta());
Token prev = null;
List<FulledNameToken> names = new ArrayList<FulledNameToken>();
do {
token = nextToken(iterator);
if (token instanceof NameToken) {
names.add(analyzer.getRealName((NameToken) token));
} else if (token instanceof CommaToken) {
if (!(prev instanceof NameToken))
unexpectedToken(token);
} else if (isOpenedBrace(token, BraceExprToken.Kind.BLOCK)) {
iterator.previous();
break;
} else
unexpectedToken(token);
prev = token;
} while (true);
implement.setNames(names);
result.setImplement(implement);
}
use of org.develnext.jphp.core.tokenizer.token.Token in project jphp by jphp-compiler.
the class FunctionGenerator method processBody.
protected void processBody(FunctionStmtToken result, ListIterator<Token> iterator) {
Token next = nextToken(iterator);
if (isOpenedBrace(next, BraceExprToken.Kind.BLOCK)) {
BodyStmtToken body = analyzer.generator(BodyGenerator.class).getToken(next, iterator);
result.setBody(body);
} else if (next instanceof SemicolonToken) {
result.setInterfacable(true);
result.setBody(null);
} else
unexpectedToken(next);
}
use of org.develnext.jphp.core.tokenizer.token.Token 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.Token in project jphp by jphp-compiler.
the class FunctionGenerator method processReturnHint.
protected void processReturnHint(FunctionStmtToken result, ListIterator<Token> iterator) {
Token token = nextTokenAndPrev(iterator);
if (token instanceof ColonToken) {
if (result.getReturnHintTypeClass() != null || result.getReturnHintType() != null) {
unexpectedToken(token);
}
nextToken(iterator);
Token next = nextToken(iterator);
if (next instanceof ValueIfElseToken) {
result.setReturnOptional(true);
next = nextToken(iterator);
}
HintType hintType;
NameToken hintTypeClass = null;
if (next instanceof SelfExprToken) {
if (analyzer.getClazz() == null) {
result.setReturnHintType(HintType.SELF);
} else {
if (analyzer.getClazz().isTrait()) {
result.setReturnHintType(HintType.SELF);
} else {
result.setReturnHintTypeClass(new FulledNameToken(next.getMeta(), new ArrayList<Token>() {
{
if (analyzer.getClazz().getNamespace().getName() != null) {
addAll(analyzer.getClazz().getNamespace().getName().getNames());
}
add(analyzer.getClazz().getName());
}
}));
}
}
} else if (next instanceof NameToken) {
String word = ((NameToken) next).getName().toLowerCase();
if (scalarTypeHints.contains(word)) {
hintType = HintType.of(word);
} else {
hintType = jphp_scalarTypeHints.contains(word) ? null : HintType.of(word);
if (hintType == null) {
hintTypeClass = analyzer.getRealName((NameToken) next);
}
}
result.setReturnHintType(hintType);
result.setReturnHintTypeClass(hintTypeClass);
} else {
unexpectedToken(next, "name");
}
}
}
Aggregations