Skip to content

Commit

Permalink
feat: get probability parsing added, for #6
Browse files Browse the repository at this point in the history
  • Loading branch information
nlf committed Oct 19, 2023
1 parent 463f557 commit 1474374
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
35 changes: 33 additions & 2 deletions lib/parser/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,30 @@ export function parseMitigation(mitigation?: MitigationInput): MitigationOutput
return result;
}

export type ProbabilityInput = "(high)" | "(H)" | ("medium") | "(M)" | "(low)" | "(L)";
export interface ProbabilityOutput {
probability: "high" | "medium" | "low";
}
export function parseProbability(probability?: ProbabilityInput): ProbabilityOutput | undefined {
if (!probability) {
return undefined;
}

if (["(high)", "(H)"].includes(probability)) {
return { probability: "high" };
}

if (["(medium)", "(M)"].includes(probability)) {
return { probability: "medium" };
}

if (["(low)", "(L)"].includes(probability)) {
return { probability: "low" };
}

return undefined;
}

export interface RawChild {
and?: Child;
or?: Child;
Expand Down Expand Up @@ -63,12 +87,14 @@ export interface ChildInput {
boolean: BooleanInput;
children?: RawChild[];
mitigation?: MitigationInput;
probability?: ProbabilityInput;
}

export interface Assumption {
assumption: string;
comments?: string[];
mitigation?: boolean;
probability?: "high" | "medium" | "low";
complete?: boolean;
}

Expand All @@ -78,16 +104,18 @@ export interface AssumptionChild {
or?: Assumption;
}

export function createAssumption({ line, boolean, mitigation, children }: ChildInput): AssumptionChild {
export function createAssumption({ line, boolean, mitigation, probability, children }: ChildInput): AssumptionChild {
const parsedLine = parseLine(line);
const parsedBoolean = parseBoolean(boolean);
const parsedMitigation = parseMitigation(mitigation);
const parsedProbability = parseProbability(probability);
const parsedChildren = parseChildren(children);

const result: AssumptionChild = {
[parsedBoolean]: {
assumption: parsedLine,
...parsedMitigation,
...parsedProbability,
...parsedChildren,
},
};
Expand All @@ -99,6 +127,7 @@ export interface Condition {
condition: string;
comments?: string[];
mitigation?: boolean;
probability?: "high" | "medium" | "low";
complete?: boolean;
}

Expand All @@ -108,16 +137,18 @@ export interface ConditionChild {
or?: Condition;
}

export function createCondition({ line, boolean, mitigation, children }: ChildInput): ConditionChild {
export function createCondition({ line, boolean, mitigation, probability, children }: ChildInput): ConditionChild {
const parsedLine = parseLine(line);
const parsedBoolean = parseBoolean(boolean);
const parsedMitigation = parseMitigation(mitigation);
const parsedProbability = parseProbability(probability);
const parsedChildren = parseChildren(children);

return {
[parsedBoolean]: {
condition: parsedLine,
...parsedMitigation,
...parsedProbability,
...parsedChildren,
},
};
Expand Down
10 changes: 6 additions & 4 deletions lib/parser/tree.peggy
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ Objective =
{ return createObjective({ line, children }) }

Assumption =
SAME_INDENT boolean:Boolean "?" WS mitigation:Mitigation? line:LineToEnd EOL?
SAME_INDENT boolean:Boolean "?" WS mitigation:Mitigation? WS probability:Probability? line:LineToEnd EOL?
children:IndentedChildren? EOL*
{ return createAssumption({ boolean, mitigation, line, children }) }
{ return createAssumption({ boolean, mitigation, probability, line, children }) }

Condition =
SAME_INDENT boolean:Boolean WS mitigation:Mitigation? line:LineToEnd EOL?
SAME_INDENT boolean:Boolean WS mitigation:Mitigation? WS probability:Probability? line:LineToEnd EOL?
children:IndentedChildren? EOL*
{ return createCondition({ boolean, mitigation, line, children }) }
{ return createCondition({ boolean, mitigation, probability, line, children }) }

Comment =
SAME_INDENT ">" WS comment:LineToEnd EOL?
Expand All @@ -55,6 +55,8 @@ Boolean = "-" / "+"

Mitigation = "[ ]" / "[x]" / "[X]"

Probability = "(high)" / "(H)" / "(medium)" / "(M)" / "(low)" / "(L)"

LineToEnd = (!EOL char:. { return char })+

WS = [ \t]*
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/sample.td
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
__Attacker's goal__
- method which in order to be viable
+ requires this condition to be true
+ (high) requires this condition to be true
+ and this condition which depends on either
- x to be true
- or y to be true
Expand Down

0 comments on commit 1474374

Please sign in to comment.