-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added check for conclusion in pattern (closes #91)
- Loading branch information
Showing
1 changed file
with
41 additions
and
9 deletions.
There are no files selected for viewing
50 changes: 41 additions & 9 deletions
50
langium/src/language/services/validation/validators/pattern-validator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,56 @@ | ||
import { diagnosticData, ValidationAcceptor } from "langium"; | ||
import { JustificationPattern } from "../../../generated/ast.js"; | ||
import { JustificationPattern, Variable } from "../../../generated/ast.js"; | ||
import { Validator } from "./abstract-validator.js"; | ||
|
||
//Class to validate patterns in the jpipe language | ||
export class PatternValidator extends Validator<JustificationPattern, "JustificationPattern">{ | ||
//function to provide validation of a given node | ||
validate(model: JustificationPattern, accept: ValidationAcceptor): void { | ||
if(model.kind === "pattern"){ | ||
let support_found = false; | ||
PatternValidator.checkConclusion(model, accept); | ||
PatternValidator.checkSupport(model, accept); | ||
} | ||
} | ||
|
||
model.variables.forEach( variable => { | ||
if(variable.kind === "@support"){ | ||
support_found = true; | ||
} | ||
}); | ||
//helper function to check existence of support variable | ||
private static checkSupport(model: JustificationPattern, accept: ValidationAcceptor): void{ | ||
let support_found = false; | ||
|
||
if(!support_found){ | ||
accept("warning", "No @support variables found in pattern", {node: model, property: "name", data: diagnosticData("noSupportInPattern")}) | ||
model.variables.forEach( variable => { | ||
if(variable.kind === "@support"){ | ||
support_found = true; | ||
} | ||
}); | ||
|
||
if(!support_found){ | ||
accept("warning", "No @support variables found in pattern", {node: model, property: "name", data: diagnosticData("noSupportInPattern")}) | ||
} | ||
} | ||
|
||
//helper function to check if conclusion exists in a pattern | ||
private static checkConclusion(model: JustificationPattern, accept: ValidationAcceptor): void{ | ||
let conclusion_exists = PatternValidator.conclusionExists(model.variables); | ||
|
||
if(!conclusion_exists){ | ||
accept("error", "All justification diagrams must have a conclusion", {node: model, property: "name", data: diagnosticData("noConclusionInPattern")}) | ||
} | ||
} | ||
|
||
//helper function to determine if a conclusion exists in a given set of variables | ||
private static conclusionExists(variables: Array<Variable>): boolean{ | ||
let conclusion_exists: boolean = false; | ||
let i = 0; | ||
|
||
while(i < variables.length && !conclusion_exists){ | ||
let variable = variables[i]; | ||
|
||
if(variable.kind === "conclusion"){ | ||
conclusion_exists = true; | ||
} | ||
|
||
i++ | ||
} | ||
|
||
return conclusion_exists; | ||
} | ||
} |