Phases WIP: add to syntax, parser, constructor, sanity (no logic yet)

parent ae88859e
......@@ -14,6 +14,7 @@
"io/ioutil"
"os"
"path"
"strconv"
"strings"
"unicode"
"unicode/utf8"
......@@ -40,6 +41,7 @@
"passive",
"active",
"principal",
"phase",
"public",
"private",
"password",
......@@ -103,7 +105,7 @@ AttackerType <-
Block <-
Comment*
Block:(Principal/Message) _
Block:(Principal/Message/Phase) _
Comment* {
return Block, nil
}
......@@ -269,6 +271,21 @@ Constants <-
return da, err
}
Phase <-
"phase" _ '[' _ Number:[0-9]+ _ ']' _ {
var err error
a := Number.([]interface{})
da := make([]uint8, len(a))
for i, v := range a { da[i] = v.([]uint8)[0] }
n, err := strconv.Atoi(b2s(da))
return block{
kind: "phase",
phase: phase{
number: n,
},
}, err
}
GuardedConstant <-
'[' GuardedConstant:[a-zA-Z0-9_]+ ']' (_ ',' _)? {
a := GuardedConstant.([]interface{})
......
......@@ -15,12 +15,15 @@ func constructKnowledgeMap(m Model, principals []string) knowledgeMap {
assigned: []value{},
creator: []string{},
knownBy: [][]map[string]string{},
phase: []int{},
unnamedCounter: 0,
}
phase := 0
valKnowledgeMap.constants = append(valKnowledgeMap.constants, constantG.constant)
valKnowledgeMap.assigned = append(valKnowledgeMap.assigned, constantG)
valKnowledgeMap.creator = append(valKnowledgeMap.creator, principals[0])
valKnowledgeMap.knownBy = append(valKnowledgeMap.knownBy, []map[string]string{})
valKnowledgeMap.phase = append(valKnowledgeMap.phase, phase)
for _, principal := range principals {
valKnowledgeMap.knownBy[0] = append(
valKnowledgeMap.knownBy[0],
......@@ -31,6 +34,7 @@ func constructKnowledgeMap(m Model, principals []string) knowledgeMap {
valKnowledgeMap.assigned = append(valKnowledgeMap.assigned, constantN)
valKnowledgeMap.creator = append(valKnowledgeMap.creator, principals[0])
valKnowledgeMap.knownBy = append(valKnowledgeMap.knownBy, []map[string]string{})
valKnowledgeMap.phase = append(valKnowledgeMap.phase, phase)
for _, principal := range principals {
valKnowledgeMap.knownBy[1] = append(
valKnowledgeMap.knownBy[1],
......@@ -43,21 +47,23 @@ func constructKnowledgeMap(m Model, principals []string) knowledgeMap {
for _, expr := range blck.principal.expressions {
switch expr.kind {
case "knows":
valKnowledgeMap = constructKnowledgeMapRenderKnows(valKnowledgeMap, blck, expr)
valKnowledgeMap = constructKnowledgeMapRenderKnows(valKnowledgeMap, blck, expr, phase)
case "generates":
valKnowledgeMap = constructKnowledgeMapRenderGenerates(valKnowledgeMap, blck, expr)
valKnowledgeMap = constructKnowledgeMapRenderGenerates(valKnowledgeMap, blck, expr, phase)
case "assignment":
valKnowledgeMap = constructKnowledgeMapRenderAssignment(valKnowledgeMap, blck, expr)
valKnowledgeMap = constructKnowledgeMapRenderAssignment(valKnowledgeMap, blck, expr, phase)
}
}
case "message":
valKnowledgeMap = constructKnowledgeMapRenderMessage(valKnowledgeMap, blck)
case "phase":
phase = blck.phase.number
}
}
return valKnowledgeMap
}
func constructKnowledgeMapRenderKnows(valKnowledgeMap knowledgeMap, blck block, expr expression) knowledgeMap {
func constructKnowledgeMapRenderKnows(valKnowledgeMap knowledgeMap, blck block, expr expression, phase int) knowledgeMap {
for _, c := range expr.constants {
i := sanityGetKnowledgeMapIndexFromConstant(valKnowledgeMap, c)
if i >= 0 {
......@@ -91,6 +97,7 @@ func constructKnowledgeMapRenderKnows(valKnowledgeMap knowledgeMap, blck block,
})
valKnowledgeMap.creator = append(valKnowledgeMap.creator, blck.principal.name)
valKnowledgeMap.knownBy = append(valKnowledgeMap.knownBy, []map[string]string{})
valKnowledgeMap.phase = append(valKnowledgeMap.phase, phase)
l := len(valKnowledgeMap.constants) - 1
if expr.qualifier != "public" {
continue
......@@ -108,7 +115,7 @@ func constructKnowledgeMapRenderKnows(valKnowledgeMap knowledgeMap, blck block,
return valKnowledgeMap
}
func constructKnowledgeMapRenderGenerates(valKnowledgeMap knowledgeMap, blck block, expr expression) knowledgeMap {
func constructKnowledgeMapRenderGenerates(valKnowledgeMap knowledgeMap, blck block, expr expression, phase int) knowledgeMap {
for _, c := range expr.constants {
i := sanityGetKnowledgeMapIndexFromConstant(valKnowledgeMap, c)
if i >= 0 {
......@@ -132,11 +139,12 @@ func constructKnowledgeMapRenderGenerates(valKnowledgeMap knowledgeMap, blck blo
})
valKnowledgeMap.creator = append(valKnowledgeMap.creator, blck.principal.name)
valKnowledgeMap.knownBy = append(valKnowledgeMap.knownBy, []map[string]string{{}})
valKnowledgeMap.phase = append(valKnowledgeMap.phase, phase)
}
return valKnowledgeMap
}
func constructKnowledgeMapRenderAssignment(valKnowledgeMap knowledgeMap, blck block, expr expression) knowledgeMap {
func constructKnowledgeMapRenderAssignment(valKnowledgeMap knowledgeMap, blck block, expr expression, phase int) knowledgeMap {
constants := sanityAssignmentConstants(expr.right, []constant{}, valKnowledgeMap)
switch expr.right.kind {
case "primitive":
......@@ -217,6 +225,7 @@ func constructKnowledgeMapRenderAssignment(valKnowledgeMap knowledgeMap, blck bl
valKnowledgeMap.assigned = append(valKnowledgeMap.assigned, expr.right)
valKnowledgeMap.creator = append(valKnowledgeMap.creator, blck.principal.name)
valKnowledgeMap.knownBy = append(valKnowledgeMap.knownBy, []map[string]string{{}})
valKnowledgeMap.phase = append(valKnowledgeMap.phase, phase)
}
return valKnowledgeMap
}
......@@ -290,6 +299,7 @@ func constructPrincipalStates(m Model, valKnowledgeMap knowledgeMap) []principal
beforeRewrite: []value{},
wasMutated: []bool{},
beforeMutate: []value{},
phase: []int{},
lock: 0,
}
for i, c := range valKnowledgeMap.constants {
......@@ -329,6 +339,7 @@ func constructPrincipalStates(m Model, valKnowledgeMap knowledgeMap) []principal
valPrincipalState.beforeRewrite = append(valPrincipalState.beforeRewrite, assigned)
valPrincipalState.wasMutated = append(valPrincipalState.wasMutated, false)
valPrincipalState.beforeMutate = append(valPrincipalState.beforeMutate, assigned)
valPrincipalState.phase = append(valPrincipalState.phase, valKnowledgeMap.phase[i])
}
valPrincipalStates = append(valPrincipalStates, valPrincipalState)
}
......@@ -349,6 +360,7 @@ func constructPrincipalStateClone(valPrincipalState principalState, purify bool)
beforeRewrite: make([]value, len(valPrincipalState.beforeRewrite)),
wasMutated: make([]bool, len(valPrincipalState.wasMutated)),
beforeMutate: make([]value, len(valPrincipalState.beforeMutate)),
phase: make([]int, len(valPrincipalState.phase)),
lock: valPrincipalState.lock,
}
copy(valPrincipalStateClone.constants, valPrincipalState.constants)
......@@ -370,5 +382,6 @@ func constructPrincipalStateClone(valPrincipalState principalState, purify bool)
} else {
copy(valPrincipalStateClone.beforeMutate, valPrincipalState.beforeMutate)
}
copy(valPrincipalStateClone.phase, valPrincipalState.phase)
return valPrincipalStateClone
}
......@@ -13,6 +13,7 @@ import (
"io/ioutil"
"os"
"path"
"strconv"
"strings"
"unicode"
"unicode/utf8"
......@@ -39,6 +40,7 @@ func reserved() []string {
"passive",
"active",
"principal",
"phase",
"public",
"private",
"password",
......@@ -71,56 +73,56 @@ var g = &grammar{
rules: []*rule{
{
name: "Verifpal",
pos: position{line: 72, col: 1, offset: 1230},
pos: position{line: 74, col: 1, offset: 1254},
expr: &actionExpr{
pos: position{line: 73, col: 2, offset: 1243},
pos: position{line: 75, col: 2, offset: 1267},
run: (*parser).callonVerifpal1,
expr: &seqExpr{
pos: position{line: 73, col: 2, offset: 1243},
pos: position{line: 75, col: 2, offset: 1267},
exprs: []interface{}{
&zeroOrMoreExpr{
pos: position{line: 73, col: 2, offset: 1243},
pos: position{line: 75, col: 2, offset: 1267},
expr: &ruleRefExpr{
pos: position{line: 73, col: 2, offset: 1243},
pos: position{line: 75, col: 2, offset: 1267},
name: "Comment",
},
},
&labeledExpr{
pos: position{line: 74, col: 2, offset: 1253},
pos: position{line: 76, col: 2, offset: 1277},
label: "Attacker",
expr: &ruleRefExpr{
pos: position{line: 74, col: 11, offset: 1262},
pos: position{line: 76, col: 11, offset: 1286},
name: "Attacker",
},
},
&labeledExpr{
pos: position{line: 75, col: 2, offset: 1272},
pos: position{line: 77, col: 2, offset: 1296},
label: "Blocks",
expr: &oneOrMoreExpr{
pos: position{line: 75, col: 10, offset: 1280},
pos: position{line: 77, col: 10, offset: 1304},
expr: &ruleRefExpr{
pos: position{line: 75, col: 10, offset: 1280},
pos: position{line: 77, col: 10, offset: 1304},
name: "Block",
},
},
},
&labeledExpr{
pos: position{line: 76, col: 2, offset: 1289},
pos: position{line: 78, col: 2, offset: 1313},
label: "Queries",
expr: &ruleRefExpr{
pos: position{line: 76, col: 10, offset: 1297},
pos: position{line: 78, col: 10, offset: 1321},
name: "Queries",
},
},
&zeroOrMoreExpr{
pos: position{line: 77, col: 2, offset: 1306},
pos: position{line: 79, col: 2, offset: 1330},
expr: &ruleRefExpr{
pos: position{line: 77, col: 2, offset: 1306},
pos: position{line: 79, col: 2, offset: 1330},
name: "Comment",
},
},
&ruleRefExpr{
pos: position{line: 78, col: 2, offset: 1316},
pos: position{line: 80, col: 2, offset: 1340},
name: "EOF",
},
},
......@@ -129,50 +131,50 @@ var g = &grammar{
},
{
name: "Attacker",
pos: position{line: 92, col: 1, offset: 1623},
pos: position{line: 94, col: 1, offset: 1647},
expr: &actionExpr{
pos: position{line: 93, col: 2, offset: 1636},
pos: position{line: 95, col: 2, offset: 1660},
run: (*parser).callonAttacker1,
expr: &seqExpr{
pos: position{line: 93, col: 2, offset: 1636},
pos: position{line: 95, col: 2, offset: 1660},
exprs: []interface{}{
&litMatcher{
pos: position{line: 93, col: 2, offset: 1636},
pos: position{line: 95, col: 2, offset: 1660},
val: "attacker",
ignoreCase: false,
},
&ruleRefExpr{
pos: position{line: 93, col: 13, offset: 1647},
pos: position{line: 95, col: 13, offset: 1671},
name: "_",
},
&litMatcher{
pos: position{line: 93, col: 15, offset: 1649},
pos: position{line: 95, col: 15, offset: 1673},
val: "[",
ignoreCase: false,
},
&ruleRefExpr{
pos: position{line: 93, col: 19, offset: 1653},
pos: position{line: 95, col: 19, offset: 1677},
name: "_",
},
&labeledExpr{
pos: position{line: 94, col: 2, offset: 1656},
pos: position{line: 96, col: 2, offset: 1680},
label: "Type",
expr: &ruleRefExpr{
pos: position{line: 94, col: 7, offset: 1661},
pos: position{line: 96, col: 7, offset: 1685},
name: "AttackerType",
},
},
&ruleRefExpr{
pos: position{line: 94, col: 20, offset: 1674},
pos: position{line: 96, col: 20, offset: 1698},
name: "_",
},
&litMatcher{
pos: position{line: 95, col: 2, offset: 1677},
pos: position{line: 97, col: 2, offset: 1701},
val: "]",
ignoreCase: false,
},
&ruleRefExpr{
pos: position{line: 95, col: 6, offset: 1681},
pos: position{line: 97, col: 6, offset: 1705},
name: "_",
},
},
......@@ -181,20 +183,20 @@ var g = &grammar{
},
{
name: "AttackerType",
pos: position{line: 99, col: 1, offset: 1708},
pos: position{line: 101, col: 1, offset: 1732},
expr: &actionExpr{
pos: position{line: 100, col: 2, offset: 1725},
pos: position{line: 102, col: 2, offset: 1749},
run: (*parser).callonAttackerType1,
expr: &choiceExpr{
pos: position{line: 100, col: 3, offset: 1726},
pos: position{line: 102, col: 3, offset: 1750},
alternatives: []interface{}{
&litMatcher{
pos: position{line: 100, col: 3, offset: 1726},
pos: position{line: 102, col: 3, offset: 1750},
val: "active",
ignoreCase: false,
},
&litMatcher{
pos: position{line: 100, col: 12, offset: 1735},
pos: position{line: 102, col: 12, offset: 1759},
val: "passive",
ignoreCase: false,
},
......@@ -204,45 +206,49 @@ var g = &grammar{
},
{
name: "Block",
pos: position{line: 104, col: 1, offset: 1781},
pos: position{line: 106, col: 1, offset: 1805},
expr: &actionExpr{
pos: position{line: 105, col: 2, offset: 1791},
pos: position{line: 107, col: 2, offset: 1815},
run: (*parser).callonBlock1,
expr: &seqExpr{
pos: position{line: 105, col: 2, offset: 1791},
pos: position{line: 107, col: 2, offset: 1815},
exprs: []interface{}{
&zeroOrMoreExpr{
pos: position{line: 105, col: 2, offset: 1791},
pos: position{line: 107, col: 2, offset: 1815},
expr: &ruleRefExpr{
pos: position{line: 105, col: 2, offset: 1791},
pos: position{line: 107, col: 2, offset: 1815},
name: "Comment",
},
},
&labeledExpr{
pos: position{line: 106, col: 2, offset: 1801},
pos: position{line: 108, col: 2, offset: 1825},
label: "Block",
expr: &choiceExpr{
pos: position{line: 106, col: 9, offset: 1808},
pos: position{line: 108, col: 9, offset: 1832},
alternatives: []interface{}{
&ruleRefExpr{
pos: position{line: 106, col: 9, offset: 1808},
pos: position{line: 108, col: 9, offset: 1832},
name: "Principal",
},
&ruleRefExpr{
pos: position{line: 106, col: 19, offset: 1818},
pos: position{line: 108, col: 19, offset: 1842},
name: "Message",
},
&ruleRefExpr{
pos: position{line: 108, col: 27, offset: 1850},
name: "Phase",
},
},
},
},
&ruleRefExpr{
pos: position{line: 106, col: 28, offset: 1827},
pos: position{line: 108, col: 34, offset: 1857},
name: "_",
},
&zeroOrMoreExpr{
pos: position{line: 107, col: 2, offset: 1830},
pos: position{line: 109, col: 2, offset: 1860},
expr: &ruleRefExpr{
pos: position{line: 107, col: 2, offset: 1830},
pos: position{line: 109, col: 2, offset: 1860},
name: "Comment",
},
},
......@@ -252,61 +258,61 @@ var g = &grammar{
},
{
name: "Principal",
pos: position{line: 111, col: 1, offset: 1865},
pos: position{line: 113, col: 1, offset: 1895},
expr: &actionExpr{
pos: position{line: 112, col: 2, offset: 1879},
pos: position{line: 114, col: 2, offset: 1909},
run: (*parser).callonPrincipal1,
expr: &seqExpr{
pos: position{line: 112, col: 2, offset: 1879},
pos: position{line: 114, col: 2, offset: 1909},
exprs: []interface{}{
&litMatcher{
pos: position{line: 112, col: 2, offset: 1879},
pos: position{line: 114, col: 2, offset: 1909},
val: "principal",
ignoreCase: false,
},
&ruleRefExpr{
pos: position{line: 112, col: 14, offset: 1891},
pos: position{line: 114, col: 14, offset: 1921},
name: "_",
},
&labeledExpr{
pos: position{line: 112, col: 16, offset: 1893},
pos: position{line: 114, col: 16, offset: 1923},
label: "Name",
expr: &ruleRefExpr{
pos: position{line: 112, col: 21, offset: 1898},
pos: position{line: 114, col: 21, offset: 1928},
name: "PrincipalName",
},
},
&ruleRefExpr{
pos: position{line: 112, col: 35, offset: 1912},
pos: position{line: 114, col: 35, offset: 1942},
name: "_",
},
&litMatcher{
pos: position{line: 112, col: 37, offset: 1914},
pos: position{line: 114, col: 37, offset: 1944},
val: "[",
ignoreCase: false,
},
&ruleRefExpr{
pos: position{line: 112, col: 41, offset: 1918},
pos: position{line: 114, col: 41, offset: 1948},
name: "_",
},
&labeledExpr{
pos: position{line: 113, col: 2, offset: 1921},
pos: position{line: 115, col: 2, offset: 1951},
label: "Expressions",
expr: &zeroOrMoreExpr{
pos: position{line: 113, col: 15, offset: 1934},
pos: position{line: 115, col: 15, offset: 1964},
expr: &ruleRefExpr{
pos: position{line: 113, col: 15, offset: 1934},
pos: position{line: 115, col: 15, offset: 1964},
name: "Expression",
},
},
},
&litMatcher{
pos: position{line: 114, col: 2, offset: 1948},
pos: position{line: 116, col: 2, offset: 1978},
val: "]",
ignoreCase: false,
},
&ruleRefExpr{
pos: position{line: 114, col: 6, offset: 1952},
pos: position{line: 116, col: 6, offset: 1982},
name: "_",
},
},
......@@ -315,17 +321,17 @@ var g = &grammar{
},
{
name: "PrincipalName",
pos: position{line: 137, col: 1, offset: 2484},
pos: position{line: 139, col: 1, offset: 2514},
expr: &actionExpr{
pos: position{line: 138, col: 2, offset: 2502},
pos: position{line: 140, col: 2, offset: 2532},
run: (*parser).callonPrincipalName1,
expr: &labeledExpr{
pos: position{line: 138, col: 2, offset: 2502},
pos: position{line: 140, col: 2, offset: 2532},
label: "Name",
expr: &oneOrMoreExpr{
pos: position{line: 138, col: 7, offset: 2507},
pos: position{line: 140, col: 7, offset: 2537},
expr: &charClassMatcher{
pos: position{line: 138, col: 7, offset: 2507},
pos: position{line: 140, col: 7, offset: 2537},
val: "[a-zA-Z0-9_]",
chars: []rune{'_'},
ranges: []rune{'a', 'z', 'A', 'Z', '0', '9'},
......@@ -338,25 +344,25 @@ var g = &grammar{
},
{
name: "Qualifier",
pos: position{line: 145, col: 1, offset: 2672},
pos: position{line: 147, col: 1, offset: 2702},
expr: &actionExpr{
pos: position{line: 146, col: 2, offset: 2686},
pos: position{line: 148, col: 2, offset: 2716},
run: (*parser).callonQualifier1,
expr: &choiceExpr{
pos: position{line: 146, col: 3, offset: 2687},
pos: position{line: 148, col: 3, offset: 2717},
alternatives: []interface{}{
&litMatcher{
pos: position{line: 146, col: 3, offset: 2687},
pos: position{line: 148, col: 3, offset: 2717},
val: "public",
ignoreCase: false,
},
&litMatcher{
pos: position{line: 146, col: 12, offset: 2696},
pos: position{line: 148, col: 12, offset: 2726},
val: "private",
ignoreCase: false,
},
&litMatcher{
pos: position{line: 146, col: 22, offset: 2706},
pos: position{line: 148, col: 22, offset: 2736},
val: "password",
ignoreCase: false,
},
......@@ -366,60 +372,60 @@ var g = &grammar{
},
{
name: "Message",
pos: position{line: 150, col: 1, offset: 2753},
pos: position{line: 152, col: 1, offset: 2783},
expr: &actionExpr{
pos: position{line: 151, col: 2, offset: 2765},
pos: position{line: 153, col: 2, offset: 2795},
run: (*parser).callonMessage1,
expr: &seqExpr{
pos: position{line: 151, col: 2, offset: 2765},
pos: position{line: 153, col: 2, offset: 2795},
exprs: []interface{}{
&labeledExpr{
pos: position{line: 151, col: 2, offset: 2765},
pos: position{line: 153, col: 2, offset: 2795},
label: "Sender",
expr: &ruleRefExpr{
pos: position{line: 151, col: 9, offset: 2772},
pos: position{line: 153, col: 9, offset: 2802},
name: "PrincipalName",
},
},
&ruleRefExpr{
pos: position{line: 151, col: 23, offset: 2786},
pos: position{line: 153, col: 23, offset: 2816},
name: "_",
},
&litMatcher{
pos: position{line: 152, col: 2, offset: 2789},
pos: position{line: 154, col: 2, offset: 2819},
val: "->",
ignoreCase: false,
},
&ruleRefExpr{
pos: position{line: 152, col: 7, offset: 2794},
pos: position{line: 154, col: 7, offset: 2824},
name: "_",
},
&labeledExpr{
pos: position{line: 153, col: 2, offset: 2797},
pos: position{line: 155, col: 2, offset: 2827},
label: "Recipient",
expr: &ruleRefExpr{
pos: position{line: 153, col: 12, offset: 2807},
pos: position{line: 155, col: 12, offset: 2837},
name: "PrincipalName",
},
},
&ruleRefExpr{
pos: position{line: 153, col: 26, offset: 2821},
pos: position{line: 155, col: 26, offset: 2851},
name: "_",
},
&litMatcher{
pos: position{line: 154, col: 2, offset: 2824},
pos: position{line: 156,</