-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathparser.go
51 lines (45 loc) · 1.12 KB
/
parser.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
package asn1go
import (
"bufio"
"io"
"math"
"os"
"strings"
)
func init() {
yyErrorVerbose = true
}
// ParseString parses string containing ASN.1 definitions into ASN.1 AST.
func ParseString(str string) (*ModuleDefinition, error) {
return ParseStream(strings.NewReader(str))
}
// ParseStream reads text of ASN.1 definitions from provided reader and parses it into ASN.1 AST.
func ParseStream(reader io.Reader) (*ModuleDefinition, error) {
lex := &MyLexer{}
lex.bufReader = bufio.NewReader(reader)
yyParse(lex)
if lex.err != nil {
return nil, lex.err
}
return lex.result, nil
}
// ParseFile parses ASN.1 definition file into ASN.1 AST.
func ParseFile(name string) (*ModuleDefinition, error) {
file, err := os.Open(name)
if err != nil {
return nil, err
}
defer file.Close()
return ParseStream(file)
}
func parseRealNumber(integer Number, fraction Number, exponent Number) Real {
value := float64(integer)
if fraction != 0 {
shift := float64(math.Pow10(int(math.Ceil(math.Log10(float64(fraction))))))
value += float64(fraction) / shift
}
if exponent != 0 {
value *= math.Pow10(int(exponent))
}
return Real(value)
}