Skip to content

Commit

Permalink
restructure code,
Browse files Browse the repository at this point in the history
  • Loading branch information
amirrezaask committed Oct 8, 2019
1 parent 87a7b0a commit 8a473c2
Show file tree
Hide file tree
Showing 8 changed files with 257 additions and 236 deletions.
119 changes: 0 additions & 119 deletions engine/parser/helpers.go

This file was deleted.

5 changes: 0 additions & 5 deletions engine/parser/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,3 @@ func Test_isComment(t *testing.T) {
assert.True(t, isComment(`//fmt.Println("Hello")`))
assert.False(t, isComment(`fmt.Println("Hello")`))
}
func Test_checkIfErrIsNotDecl(t *testing.T) {
assert.True(t, checkIfErrIsNotDecl(`"fmt" imported and not used`))
assert.True(t, checkIfErrIsNotDecl(`a declared and not used`))
assert.False(t, checkIfErrIsNotDecl("not able to compile"))
}
114 changes: 102 additions & 12 deletions engine/parser/parser.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package parser

import (
"regexp"
"strings"
)

type StmtType uint8

const (
Expand All @@ -12,28 +17,113 @@ const (
StmtTypeVarDecl
StmtTypeFuncDecl
StmtUnknown
StmtEmpty
)

type ParseResult struct {
StmtType StmtType
}
func Parse(code string) (StmtType, bool, error) {

func Parse(code string) (StmtType, error) {
if len(code) < 1 {
return StmtEmpty, false, nil
}
if isComment(code) {
return StmtTypeComment, nil
return StmtTypeComment, ShouldContinue(code), nil
} else if isImport(code) {
return StmtTypeImport, nil
return StmtTypeImport, ShouldContinue(code), nil
} else if isFunc(code) {
return StmtTypeFuncDecl, nil
return StmtTypeFuncDecl, ShouldContinue(code), nil
} else if isTypeDecl(code) {
return StmtTypeTypeDecl, nil
return StmtTypeTypeDecl, ShouldContinue(code), nil
} else if isPrint(code) {
return StmtTypePrint, nil
return StmtTypePrint, ShouldContinue(code), nil
} else if isComment(code) {
return StmtTypeComment, nil
return StmtTypeComment, ShouldContinue(code), nil
} else if isExpr(code) {
return StmtTypeExpr, nil
return StmtTypeExpr, ShouldContinue(code), nil
} else {
return StmtUnknown, nil
return StmtUnknown, ShouldContinue(code), nil
}
}
func ShouldContinue(code string) bool {
var stillOpenChars int
for _, c := range code {
if c == '{' || c == '(' {
stillOpenChars++
continue
}
if c == '}' || c == ')' {
stillOpenChars--
}
}
return stillOpenChars > 0
}
func isComment(code string) bool {
if code[:2] == "//" || code[:2] == "/*" {
return true
}
return false
}

func isShellCommand(code string) bool {
if len(code) == 0 {
return false
}
return code[0] == ':'
}

func isTypeDecl(code string) bool {
matched, err := regexp.Match("type .+", []byte(code))
if err != nil {
return false
}
return matched
}
func reSubMatchMap(r *regexp.Regexp, str string) map[string]string {
match := r.FindStringSubmatch(str)
subMatchMap := make(map[string]string)
for i, name := range r.SubexpNames() {
if i != 0 {
subMatchMap[name] = match[i]
}
}

return subMatchMap
}
func isFunctionCall(code string) bool {
m, err := regexp.Match("^[a-zA-Z0-9_.-]+\\(.*\\)", []byte(code))
if err != nil {
return false
}
return m && strings.ContainsAny(code, "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm")
}

func isExpr(code string) bool {
if (strings.Contains(code, "=") && !strings.Contains(code, "==")) || strings.Contains(code, "var") || isFunctionCall(code) {
return false
}
return true
}
func isFunc(code string) bool {
matched, err := regexp.Match("^func.+", []byte(code))
if err != nil {
return false
}
return matched
}
func isImport(im string) bool {
matched, err := regexp.Match("import .+", []byte(im))
if err != nil {
panic(err)
}
return matched
}
func isPrint(code string) bool {
matched1, err := regexp.Match("^fmt.Print.*\\(.*\\)", []byte(code))
if err != nil {
panic(err)
}
matched2, err := regexp.Match("^print(ln|f).*", []byte(code))
if err != nil {
panic(err)
}
return matched1 || matched2
}
Loading

0 comments on commit 8a473c2

Please sign in to comment.