-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse.go
179 lines (171 loc) · 4.92 KB
/
parse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright 2023. Silvano DAL ZILIO (LAAS-CNRS). All rights reserved. Use of
// this source code is governed by the GNU Affero license that can be found in
// the LICENSE file.
package pnml
import (
"bytes"
"encoding/xml"
"errors"
"fmt"
)
func parseExprMult(d *xml.Decoder, acc []Expression) ([]Expression, error) {
res, err := parseExprElement(d)
if err != nil {
return nil, fmt.Errorf("error while parsing a sequence of elements; %s", err)
}
if res == nil {
return acc, nil
}
return parseExprMult(d, append(acc, res))
}
func parseExprInner(decoder *xml.Decoder) (Expression, error) {
res, err := parseExprElement(decoder)
skipUntilEnd(decoder)
return res, err
}
func skipUntilEnd(d *xml.Decoder) error {
t, _ := d.Token()
if t == nil {
return fmt.Errorf("expecting an xml.EndElement")
}
switch t.(type) {
case xml.CharData:
skipUntilEnd(d)
case xml.EndElement:
return nil
}
return fmt.Errorf("expecting an xml.EndElement, found (%T) %v", t, t)
}
func parseExprElement(decoder *xml.Decoder) (Expression, error) {
t, _ := decoder.Token()
if t == nil {
return nil, nil
}
switch se := t.(type) {
case xml.CharData:
return parseExprElement(decoder)
case xml.EndElement:
return nil, nil
case xml.StartElement:
switch se.Name.Local {
case "subterm":
return parseExprInner(decoder)
case "all":
res, err := parseExprInner(decoder)
if err != nil {
return nil, err
}
if s, ok := res.(Constant); ok {
return All(s), nil
}
return nil, errors.New("malformed PNML, an all element should contain usersort")
case "usersort":
var s Type
decoder.DecodeElement(&s, &se)
return Constant(s.ID), nil
case "useroperator":
var s Type
decoder.DecodeElement(&s, &se)
return Constant(s.ID), nil
case "tuple":
ee, err := parseExprMult(decoder, nil)
if err != nil {
return nil, err
}
return Tuple(ee), nil
case "add":
ee, err := parseExprMult(decoder, nil)
if err != nil {
return nil, err
}
return Add(ee), nil
case "subtract":
// Subtract is actually like an Add since there can be several sub
// chained together, see the example in PhiloDyn, trans. Initialize,
// that has an arc pattern of the form All - p - q, where p and q
// are variables.
ee, err := parseExprMult(decoder, nil)
if err != nil {
return nil, err
}
return Subtract(ee), nil
case "successor", "predecessor":
inc := 1
if se.Name.Local == "predecessor" {
inc = -1
}
res, err := parseExprInner(decoder)
if err != nil {
return nil, err
}
if r, ok := res.(Successor); ok {
return Successor{Var: r.Var, Incr: r.Incr + inc}, nil
}
if r, ok := res.(Var); ok {
return Successor{Var: r, Incr: inc}, nil
}
return nil, errors.New("we only support successor and predecessor on variables")
case "or", "and", "equality", "inequality",
"lessthanorequal", "lessthan",
"greaterthan", "greaterthanorequal":
ee, err := parseExprMult(decoder, nil)
if err != nil {
return nil, err
}
return Operation{Op: getpOP(se.Name.Local), Elem: ee}, nil
case "dotconstant":
skipUntilEnd(decoder)
return Dot{}, nil
case "numberof":
// we consider the case where numberof does not have two subelements
// (the multiplicity is missing). This was found in one PNML model,
// SimpleLoad, and lately on instance DotAndBoxes-COL-3 found in the
// MCC website. The same problem appears in files dot2.pnml and
// dot3.pnml of the benchmarks/simple folder.
res1, _ := parseExprElement(decoder)
if numb, ok := res1.(Numberof); ok {
res2, _ := parseExprElement(decoder)
skipUntilEnd(decoder)
numb.Expression = res2
return numb, nil
}
res2, _ := parseExprElement(decoder)
if res2 == nil {
// it means that res1 was a value, not a numberconstant, and
// that the multiplicity was forgotten in the XML numberof
// element.
return Numberof{Expression: res1, Mult: 1}, nil
}
return nil, errors.New("malformed PNML in numberof, numberconstant is missing")
case "numberconstant":
var val NumberConstant
decoder.DecodeElement(&val, &se)
return Numberof{Expression: nil, Mult: val.Value}, nil
case "finiteintrangeconstant":
var val FIRangeConstant
decoder.DecodeElement(&val, &se)
return FIRConstant{
value: val.Value,
start: val.Range.Start,
end: val.Range.End,
}, nil
case "variable":
var val Variable
decoder.DecodeElement(&val, &se)
return Var(val.RefVariable), nil
default:
return nil, errors.New("malformed PNML: unexpected Token <" + se.Name.Local + "> in parseExpr")
}
}
return nil, errors.New("malformed PNML: unexpected Token in parseExpr")
}
// parseExpression returns the PNML expression corresponding to the XML content
// found in the provided byte slice.
func parseExpression(b []byte) (Expression, error) {
if len(b) == 0 {
return nil, nil
}
buff := bytes.NewBuffer(b)
decoder := xml.NewDecoder(buff)
return parseExprElement(decoder)
}