Feature: NeoLang supports quoted string

This commit is contained in:
zt515
2017-08-02 23:28:52 +08:00
parent 1698d78439
commit 62ca97ae5b
16 changed files with 262 additions and 71 deletions

View File

@ -26,8 +26,11 @@ class NeoLangParser {
private fun updateParserStatus(tokens: List<NeoLangToken>) {
if (tokens.isEmpty()) {
throw ParseException("Input tokens must be non-empty")
// Allow empty program
ast = NeoLangProgramNode.emptyNode()
return
}
this.tokens.clear()
this.tokens.addAll(tokens)
currentPosition = 0
@ -48,7 +51,7 @@ class NeoLangParser {
return true
} else if (errorThrow) {
throw InvalidTokenException("Unexpected token type " +
throw InvalidTokenException("Unexpected token `${currentToken.tokenValue}' typed " +
"`${currentToken.tokenType}' near line ${currentToken.lineNumber}, " +
"expected $tokenType")
}
@ -98,7 +101,7 @@ class NeoLangParser {
private fun attribute(): NeoLangAttributeNode? {
val token = currentToken ?: throw InvalidTokenException("Unexpected token: null")
if (match(NeoLangTokenType.STRING)) {
if (match(NeoLangTokenType.ID)) {
match(NeoLangTokenType.COLON, errorThrow = true)
val block = block() ?: NeoLangBlockNode.emptyNode()
return NeoLangAttributeNode(NeoLangStringNode(token), block)
@ -113,6 +116,10 @@ class NeoLangParser {
match(NeoLangTokenType.NUMBER, errorThrow = true)
return NeoLangBlockNode(NeoLangNumberNode(token))
}
NeoLangTokenType.ID -> {
match(NeoLangTokenType.ID, errorThrow = true)
return NeoLangBlockNode(NeoLangStringNode(token))
}
NeoLangTokenType.STRING -> {
match(NeoLangTokenType.STRING, errorThrow = true)
return NeoLangBlockNode(NeoLangStringNode(token))
@ -127,7 +134,7 @@ class NeoLangParser {
}
else -> throw InvalidTokenException("Unexpected token `$token' for block, " +
"expected `${NeoLangTokenType.NUMBER}', `${NeoLangTokenType.STRING}' or `${NeoLangTokenType.BRACKET_START}'")
"expected `${NeoLangTokenType.NUMBER}', `${NeoLangTokenType.ID}' or `${NeoLangTokenType.BRACKET_START}'")
}
}