Skip to content

Commit

Permalink
Merge pull request #799 from shamanas/invalid-modifiers
Browse files Browse the repository at this point in the history
Check for function modifier placement/validity.
Closes #797
  • Loading branch information
alexnask committed Aug 18, 2014
2 parents bbb4471 + 5acd53b commit c77aff1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
29 changes: 29 additions & 0 deletions source/rock/frontend/AstBuilder.ooc
Expand Up @@ -33,6 +33,10 @@ IllegalTypeArgError: class extends Error {
init: super func ~tokenMessage
}

InvalidModifierError: class extends Error {
init: super func ~tokenMessage
}

computeReservedHashs: func (words: String[]) -> ArrayList<Int> {
list := ArrayList<Int> new()
words length times(|i|
Expand Down Expand Up @@ -631,6 +635,8 @@ AstBuilder: class {
}

onOperatorAbstract: unmangled(nq_onOperatorAbstract) func {
checkModifierValidity("abstract")

peek(OperatorDecl) setAbstract(true)
}

Expand Down Expand Up @@ -663,6 +669,19 @@ AstBuilder: class {
* Functions
*/

checkModifierValidity: func(mod: String) {
temp := pop(Node)

match (peek(Node)) {
case tDecl: TypeDecl =>
// All is fine :D
case =>
params errorHandler onError(InvalidModifierError new(temp token, "Invalid use of modifier #{mod} outside of a type declaration"))
}

stack push(temp)
}

onFunctionStart: unmangled(nq_onFunctionStart) func (name, doc: CString) {
fDecl := FunctionDecl new(name toString(), token())
fDecl setVersion(getVersion())
Expand All @@ -679,19 +698,27 @@ AstBuilder: class {
}

onFunctionAbstract: unmangled(nq_onFunctionAbstract) func {
checkModifierValidity("abstract")

peek(FunctionDecl) isAbstract = true
}
onFunctionThisRef: unmangled(nq_onFunctionThisRef) func {
checkModifierValidity("@")

peek(FunctionDecl) isThisRef = true
}
onFunctionStatic: unmangled(nq_onFunctionStatic) func {
checkModifierValidity("static")

peek(FunctionDecl) isStatic = true
}
onFunctionInline: unmangled(nq_onFunctionInline) func {
peek(FunctionDecl) isInline = true
}

onFunctionFinal: unmangled(nq_onFunctionFinal) func {
checkModifierValidity("final")

peek(FunctionDecl) isFinal = true
}

Expand All @@ -700,6 +727,8 @@ AstBuilder: class {
}

onFunctionSuper: unmangled(nq_onFunctionSuper) func {
checkModifierValidity("super")

peek(FunctionDecl) isSuper = true
}

Expand Down
5 changes: 5 additions & 0 deletions test/compiler/functions/invalid-abstract-outside-class.ooc
@@ -0,0 +1,5 @@
//! shouldfail

f: abstract func(i: Int) -> Int {
i + 1
}
4 changes: 4 additions & 0 deletions test/compiler/operators/invalid-abstract-overload.ooc
@@ -0,0 +1,4 @@
//! shouldfail

abstract operator + (l: Bool, right: Int) -> String

0 comments on commit c77aff1

Please sign in to comment.