/
parser.go
110 lines (92 loc) · 3 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
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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package parser
import (
"bufio"
"encoding/json"
"github.com/ErikPelli/TL-parser-JSON/parser/jsonStruct"
"os"
)
func Parse (file *os.File) error {
// Remove old schema or create a new folder if not exists
_ = os.Remove("result/schema.json")
_ = os.Mkdir("result", os.ModeDir)
// Create a new output file
output, err := os.OpenFile("result/schema.json", os.O_APPEND|os.O_RDWR|os.O_CREATE, 666)
if err != nil {
return err
}
defer output.Close()
// Slices to convert to jsonStruct
constructors := make([]*jsonStruct.Constructor, 0)
methods := make([]*jsonStruct.Method, 0)
// Buffer I/O Scanner from TL input file
scanner := bufio.NewScanner(file)
// Current mode (constructor/method)
methodMode := false // First mode is always constructor
// Parse every line
for scanner.Scan() {
// Get line as string
line := scanner.Text()
// Iteration check
if line == "---functions---" {
// Set mode to function
methodMode = true
continue
} else if line == "---types---" {
// Set mode to constructor
methodMode = false
continue
} else if line == "" || (line[0] == '/' && line[1] == '/') {
// If this line is useless (empty or a comment), skip it
continue
}
// Parse line to single jsonStruct object and add it to corresponding slice
if methodMode {
// Create a new method
method := new(jsonStruct.Method)
// Fill method
method.Id = getID(line)
method.Method = getName(line)
method.Params = getParams(line)
method.Type = getType(line)
// Add method to slice
methods = append(methods, method)
} else {
// Create a new constructor
constructor := new(jsonStruct.Constructor)
// Fill constructor
constructor.Id = getID(line)
constructor.Predicate = getName(line)
constructor.Params = getParams(line)
constructor.Type = getType(line)
// Add constructor to slice
constructors = append(constructors, constructor)
}
}
// Create a Schema struct
TLStruct := new(jsonStruct.Schema)
TLStruct.Constructors = constructors
TLStruct.Methods = methods
// Convert struct to json
jsonBytes, err := json.Marshal(TLStruct); if err != nil {return err}
// Write json to file
_, err = output.Write(jsonBytes); if err != nil {return err}
return nil
}