Skip to content

Commit

Permalink
Merge pull request #2 from OnionGrief/master
Browse files Browse the repository at this point in the history
Попытка в рефал
  • Loading branch information
xtoter authored Mar 1, 2024
2 parents 9e79410 + c52fa01 commit 7bed925
Show file tree
Hide file tree
Showing 16 changed files with 309 additions and 84 deletions.
3 changes: 3 additions & 0 deletions bin/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/OnionGrief/AlliumLinter

go 1.21.0
Binary file modified bin/refalLint
Binary file not shown.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module refalLint
module github.com/OnionGrief/AlliumLinter

go 1.21

Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"refalLint/src/cmd"
"github.com/OnionGrief/AlliumLinter/src/cmd"
)

func main() {
Expand Down
42 changes: 27 additions & 15 deletions src/checks/precondition.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package checks

import (
"fmt"
"refalLint/src/config"
"refalLint/src/lexer"
"refalLint/src/logger"
"github.com/OnionGrief/AlliumLinter/src/config"
"github.com/OnionGrief/AlliumLinter/src/lexer"
"github.com/OnionGrief/AlliumLinter/src/logger"
"unicode"
)

Expand All @@ -30,27 +30,41 @@ func checkConstCount(tokens []lexer.Token) []logger.Log {
if leng < 3 {
leng = 3
}
chks := make(map[string]uint)
chks := make(map[string][]lexer.Position)
str := ""
var pos lexer.Position
for _, tkn := range tokens {
switch tkn.TokenType {
case lexer.ASCIIV:
if str == "" {
pos = tkn.Start
}
str += tkn.Value
default:
if str != "" {
if len(str) > int(leng) {
chks[str]++
if chks[str] > count {
logs = append(logs, logger.ExternLog(fmt.Sprintf("%s must be a const %d:%d", str, tkn.Start, tkn.Finish)))
}
chks[str] = append(chks[str], pos)
}
str = ""
}
}
}
for key, val := range chks {
if len(val) > int(count) {
logs = append(logs, logger.ExternLog(fmt.Sprintf("%s must be a const %s", key, prepareCoord(val))))
}
}
return logs
}

func prepareCoord(in []lexer.Position) string {
str := ""
for _, val := range in {
str = fmt.Sprintf("%s (%d:%d)", str, val.Row, val.Column)
}
return str
}

// Проверяем на CamelCase или SnakeCase
func checkNames(tokens []lexer.Token) []logger.Log {
var logs []logger.Log
Expand All @@ -77,17 +91,15 @@ func checkNames(tokens []lexer.Token) []logger.Log {
}
func checkExtern(tokens []lexer.Token) []logger.Log {
var logs []logger.Log
var extrn *lexer.Token
var pos []lexer.Position
for _, tkn := range tokens {
if tkn.TokenType == lexer.EXTERN {
if extrn == nil {
extrn = &tkn
} else {
logs = append(logs, logger.ExternLog(fmt.Sprintf("Обнаружено несколько EXTERN, рекомендуется использовать 1 %d:%d (Первое использование %d:%d)", extrn.Start.Column, extrn.Start.Column, tkn.Start.Column, tkn.Start.Column)))
return logs
}
pos = append(pos, tkn.Start)
}
}
if len(pos) > 1 {
logs = append(logs, logger.ExternLog(fmt.Sprintf("Обнаружено несколько EXTERN, рекомендуется использовать 1 %s", prepareCoord(pos))))
}
return logs
}

Expand Down
4 changes: 2 additions & 2 deletions src/cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package cmd

import (
"github.com/OnionGrief/AlliumLinter/src/cmd/lint"
"github.com/OnionGrief/AlliumLinter/src/tree"
"github.com/spf13/cobra"
"refalLint/src/cmd/lint"
"refalLint/src/tree"
)

var RootCmd = &cobra.Command{
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/debug/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package main
import (
"bufio"
"fmt"
"github.com/OnionGrief/AlliumLinter/src/lexer"
"github.com/OnionGrief/AlliumLinter/src/tree"
"os"
"refalLint/src/lexer"
"refalLint/src/tree"
)

// Пакет используется строго для дебага дерева, не собирайте его
Expand Down
7 changes: 4 additions & 3 deletions src/cmd/lint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package lint

import (
"fmt"
"github.com/OnionGrief/AlliumLinter/src/config"
"github.com/OnionGrief/AlliumLinter/src/linter"
"github.com/OnionGrief/AlliumLinter/src/out"
"io/ioutil"
"log"
"os"
"path/filepath"
"refalLint/src/config"
"refalLint/src/linter"
"refalLint/src/out"
"strings"
"sync"

Expand Down Expand Up @@ -74,6 +74,7 @@ func init() {
LintCmd.Flags().BoolVarP(&config.CamelCase, "camel", "c", false, "Use CamelCase for format")
LintCmd.Flags().UintVarP(&config.ConstLen, "constLen", "L", 3, "ConstLen")
LintCmd.Flags().UintVarP(&config.ConstCount, "constCount", "C", 3, "ConstCount")
LintCmd.Flags().UintVarP(&config.BlockLen, "blockLen", "b", 3, "Длина переиспользуемого блока")
}

func prepareFiles(files []string) {
Expand Down
5 changes: 5 additions & 0 deletions src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ var (
ConstCount uint
ConstLen uint
PrintTree bool
BlockLen uint
)

const FuncComment = "OPT:"

var UsingFunctions []string = []string{"Go"}

var CountVar = 3
39 changes: 15 additions & 24 deletions src/lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ func (l lexer) GetToken() (res []Token, err error) {
case ' ':
return []Token{NewTokenByType(SPACE, symb.pos, symb.pos)}, nil
case '$':
//cringe
symb1 := l.readChar()
if symb1.EOF {
return []Token{}, ErrInLexer
Expand Down Expand Up @@ -181,7 +180,7 @@ func (l lexer) GetToken() (res []Token, err error) {
if symb3.symbol == '/' {
return []Token{NewTokenByTypeAndVal(BIGCOMMENT, comment, symb.pos, symb3.pos)}, nil
} else {
comment += "*" + string(symb3.symbol)
l.info.needPrevious = true
}
} else {
comment += string(symb2.symbol)
Expand All @@ -191,29 +190,21 @@ func (l lexer) GetToken() (res []Token, err error) {
return []Token{}, ErrInLexer
}
case '*':
symb1 := l.readChar()
if symb1.EOF {
return []Token{}, ErrInLexer
}
if symb1.symbol == '$' {
comment := ""
symb2 := symb1
for {
pos1 := symb2.pos
symb2 = l.readChar()
if symb2.EOF {
return []Token{}, ErrInLexer
}
if symb2.symbol == '\n' {
l.info.needPrevious = true
return []Token{NewTokenByTypeAndVal(COMMENT, comment, symb.pos, pos1)}, nil
} else {
comment += string(symb2.symbol)
}
comment := ""
for {
pos1 := symb.pos
symb2 := l.readChar()
if symb2.EOF {
return []Token{}, ErrInLexer
}
if symb2.symbol == '\n' {
l.info.needPrevious = true
return []Token{NewTokenByTypeAndVal(COMMENT, comment, symb.pos, pos1)}, nil
} else {
comment += string(symb2.symbol)
}
} else {
return []Token{}, ErrInLexer
}

case 's', 't', 'e':
symb1 := l.readChar()
if symb1.EOF {
Expand Down Expand Up @@ -329,7 +320,7 @@ func (l lexer) GetToken() (res []Token, err error) {
if symb1.EOF {
return []Token{}, ErrInLexer
}
if isAlpha(symb1.symbol) {
if isAlnum(symb1.symbol) {
str += string(symb1.symbol)
} else {
l.info.needPrevious = true
Expand Down
4 changes: 4 additions & 0 deletions src/lexer/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ type Position struct {
Column uint
Row uint
}

type Coords struct {
start, end Position
}
type Token struct {
TokenType TokenType
Value string
Expand Down
10 changes: 5 additions & 5 deletions src/linter/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package linter
import (
"bufio"
"fmt"
"github.com/OnionGrief/AlliumLinter/src/checks"
"github.com/OnionGrief/AlliumLinter/src/lexer"
"github.com/OnionGrief/AlliumLinter/src/logger"
"github.com/OnionGrief/AlliumLinter/src/tree"
"os"
"refalLint/src/checks"
"refalLint/src/lexer"
"refalLint/src/logger"
"refalLint/src/tree"
"sync"
"time"
)
Expand Down Expand Up @@ -39,7 +39,6 @@ func LintFile(filePath string, results chan<- Result, wg *sync.WaitGroup) {
results <- Result{FilePath: filePath, Error: err}
return
}
fmt.Println("get all tokens ok")
logs := checks.CheckPrecondition(tkns)
out = append(out, logs...)
psr := tree.NewParser(tkns)
Expand All @@ -51,6 +50,7 @@ func LintFile(filePath string, results chan<- Result, wg *sync.WaitGroup) {
logs2 := psr.Checks()
out = append(out, logs2...)
node = node
out = append(out, tree.CheckTreeRec(node)...)
// После проверки отправляем результат в канал
results <- Result{FilePath: filePath, Success: true, Logs: out}
}
Expand Down
22 changes: 21 additions & 1 deletion src/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package logger

import (
"fmt"
"refalLint/src/lexer"
"github.com/OnionGrief/AlliumLinter/src/lexer"
"strings"
)

type logType uint
Expand Down Expand Up @@ -55,9 +56,28 @@ func UnreachableLog(tkn lexer.Token) Log {
}
}
func CodeInComment(tkn lexer.Token) Log {
if strings.Contains(tkn.Value, "\n") {
strs := strings.Split(tkn.Value, "\n")
if strs[0] == "" {
strs = strs[1:]
}
return Log{
str: fmt.Sprintf("Код в комментарии %s (%d:%d-%d:%d)", strs[0], tkn.Start.Column, tkn.Start.Row, tkn.Finish.Column, tkn.Finish.Row),
lgType: extern,
level: Warning,
}
}
return Log{
str: fmt.Sprintf("Код в комментарии %s (%d:%d-%d:%d)", tkn.Value, tkn.Start.Column, tkn.Start.Row, tkn.Finish.Column, tkn.Finish.Row),
lgType: extern,
level: Warning,
}

}
func ReusingBlock(len int, pos1, pos2 lexer.Position) Log {
return Log{
str: fmt.Sprintf("Найден переиспользуемый блок длины %d [%d:%d] [%d:%d]", len, pos1.Column, pos1.Row, pos2.Column, pos2.Row),
lgType: extern,
level: Warning,
}
}
Loading

0 comments on commit 7bed925

Please sign in to comment.