Now splits by windows and unix new lines, added parsing structure.

This commit is contained in:
Marvin Blum
2015-12-17 23:23:49 +01:00
parent 05e6937201
commit 3f6b854a09
2 changed files with 122 additions and 51 deletions

View File

@@ -11,7 +11,8 @@ type Token struct {
Column int Column int
} }
var delimiter = []byte{ var (
delimiter = []byte{
'=', '=',
';', ';',
'{', '{',
@@ -32,7 +33,7 @@ var delimiter = []byte{
'*', '*',
'/'} // TODO: modulo? '/'} // TODO: modulo?
var keywords = []string{ keywords = []string{
"var", "var",
"if", "if",
"while", "while",
@@ -51,10 +52,11 @@ var keywords = []string{
"waituntil", "waituntil",
"code"} "code"}
var whitespace = []byte{' ', '\n', '\t', '\r'} whitespace = []byte{' ', '\n', '\t', '\r'}
var identifier = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_" identifier = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"
var preprocessor = byte('#') preprocessor = byte('#')
var new_line = []byte{'\r', '\n'} new_line = []byte{'\r', '\n'}
)
// Tokenizes the given byte array into syntax tokens, // Tokenizes the given byte array into syntax tokens,
// which can be parsed later. // which can be parsed later.

View File

@@ -1,12 +1,45 @@
package types package types
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"strings" "strings"
) )
const (
// type for object types
TYPE = 1
// types for functions
NULL = 2
UNARY = 3
BINARY = 4
win_new_line = "\r\n"
unix_new_line = "\n"
)
type FunctionType struct {
Name string
Type int // one of the constants NULL, UNARY, BINARY
ArgsCount int
}
var functions []FunctionType
// Returns function type information by name.
// If not found, the first parameter will be false.
func GetFunction(name string) (bool, FunctionType) {
for _, function := range functions {
if function.Name == name {
return true, function
}
}
return false, FunctionType{}
}
// Loads type information from file. // Loads type information from file.
// The format is specified by 'supportInfo' command: https://community.bistudio.com/wiki/supportInfo
func LoadTypes(path string) error { func LoadTypes(path string) error {
content, err := ioutil.ReadFile(path) content, err := ioutil.ReadFile(path)
@@ -14,19 +47,55 @@ func LoadTypes(path string) error {
return err return err
} }
if err := parseTypes(string(content)); err != nil { data := strings.Replace(win_new_line, unix_new_line, string(content), -1) // make this work on windows and unix
return err parseTypes(data)
}
return nil return nil
} }
func parseTypes(content string) error { func parseTypes(content string) {
lines := strings.Split(content, "\\n") lines := strings.Split(content, unix_new_line)
for _, line := range lines { for _, line := range lines {
fmt.Println(line) if len(line) < 3 {
continue
} }
return nil if line[0] == 'n' {
parseNullFunction(line)
} else if line[0] == 'u' {
parseUnaryFunction(line)
} else if line[0] == 'b' {
parseBinaryFunction(line)
}
}
}
func parseNullFunction(line string) {
parts := getParts(line)
for _, part := range parts {
}
}
func parseUnaryFunction(line string) {
parts := getParts(line)
for _, part := range parts {
}
}
func parseBinaryFunction(line string) {
parts := getParts(line)
for _, part := range parts {
}
}
func getParts(line string) []string {
line = line[2:]
return strings.Split(line, " ")
} }