Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cd56839

Browse files
committed
chore: built logging for parser
1 parent 9875919 commit cd56839

3 files changed

Lines changed: 121 additions & 8 deletions

File tree

parser/logger.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package parser
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"reflect"
7+
"regexp"
8+
9+
"github.com/fatih/color"
10+
)
11+
12+
type NodeLogging struct {
13+
Type string `json:"type"`
14+
Data any `json:"data"`
15+
}
16+
17+
type Logger struct {
18+
enableLogging bool
19+
}
20+
21+
func NewLogger(enableLogging bool) *Logger {
22+
return &Logger{enableLogging: enableLogging}
23+
}
24+
25+
var (
26+
colorizeKey = color.New(color.FgHiCyan).SprintFunc()
27+
colorizeString = color.New(color.FgHiYellow).SprintFunc()
28+
colorizeValue = color.New(color.FgHiMagenta).SprintFunc()
29+
)
30+
31+
func colorizeJSON(s string) string {
32+
keyRe := regexp.MustCompile(`"([^"]+)"\s*:`)
33+
s = keyRe.ReplaceAllString(s, colorizeKey(`"$1"`)+":")
34+
35+
// "string"
36+
strRe := regexp.MustCompile(`:\s*"([^"]*)"`)
37+
s = strRe.ReplaceAllString(s, ": "+colorizeString(`"$1"`))
38+
39+
// números, bool, null
40+
valRe := regexp.MustCompile(`:\s*(\d+|true|false|null)`)
41+
s = valRe.ReplaceAllString(s, ": "+colorizeValue(`$1`))
42+
43+
return s
44+
}
45+
46+
func WrapNodeLogging(n Node) any {
47+
if n == nil {
48+
return nil
49+
}
50+
51+
v := reflect.ValueOf(n)
52+
t := reflect.TypeOf(n)
53+
54+
// unwrap ponteiro
55+
if t.Kind() == reflect.Pointer {
56+
t = t.Elem()
57+
v = v.Elem()
58+
}
59+
60+
// percorre campos e reembrulha Nodes internos
61+
m := make(map[string]interface{})
62+
for i := 0; i < t.NumField(); i++ {
63+
field := t.Field(i)
64+
value := v.Field(i).Interface()
65+
66+
switch v := value.(type) {
67+
case Node:
68+
m[field.Tag.Get("json")] = WrapNodeLogging(v)
69+
70+
case []Node:
71+
arr := make([]interface{}, 0, len(v))
72+
for _, n := range v {
73+
arr = append(arr, WrapNodeLogging(n))
74+
}
75+
m[field.Tag.Get("json")] = arr
76+
77+
default:
78+
if tag := field.Tag.Get("json"); tag != "-" && tag != "" {
79+
m[tag] = value
80+
}
81+
}
82+
}
83+
84+
return NodeLogging{
85+
Type: t.Name(),
86+
Data: m,
87+
}
88+
}
89+
90+
func (l *Logger) JSON(m ModuleNode) (int, error) {
91+
if l.enableLogging {
92+
bs, err := json.MarshalIndent(WrapNodeLogging(m), "", " ")
93+
if err != nil {
94+
return 0, err
95+
}
96+
return fmt.Println(colorizeJSON(string(bs)))
97+
}
98+
return 0, nil
99+
}

parser/node.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package parser
22

3-
import "github.com/guiferpa/aurora/lexer"
3+
import (
4+
"github.com/guiferpa/aurora/lexer"
5+
)
46

57
type Node interface {
68
Next() Node
@@ -61,7 +63,7 @@ type ReelLiteralNode struct {
6163
}
6264

6365
type VoidLiteralNode struct {
64-
Token lexer.Token
66+
Token lexer.Token `json:"-"`
6567
}
6668

6769
func (vln VoidLiteralNode) Next() Node {
@@ -211,7 +213,7 @@ func (en ExpressionNode) Next() Node {
211213
}
212214

213215
type PrintStatementNode struct {
214-
Param Node `json:"param"`
216+
Param Node `json:"parameter"`
215217
}
216218

217219
func (cpsn PrintStatementNode) Next() Node {
@@ -265,3 +267,7 @@ type ModuleNode struct {
265267
Name string `json:"name"`
266268
Statements []Node `json:"statements"`
267269
}
270+
271+
func (mn ModuleNode) Next() Node {
272+
return nil
273+
}

parser/parser.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ type Parser interface {
1818
}
1919

2020
type pr struct {
21-
cursor int
22-
tokens []lexer.Token
23-
filename string
24-
enableLogging bool
21+
cursor int
22+
tokens []lexer.Token
23+
filename string
24+
logger *Logger
2525
}
2626

2727
// Helper functions to validate node types for tape operations
@@ -806,6 +806,9 @@ func (p *pr) Parse() (AST, error) {
806806
if err != nil {
807807
return AST{}, err
808808
}
809+
if _, err := p.logger.JSON(module); err != nil {
810+
return AST{}, err
811+
}
809812
return AST{module}, nil
810813
}
811814

@@ -815,5 +818,10 @@ type NewParserOptions struct {
815818
}
816819

817820
func New(tokens []lexer.Token, options NewParserOptions) Parser {
818-
return &pr{cursor: 0, tokens: tokens, filename: options.Filename, enableLogging: options.EnableLogging}
821+
return &pr{
822+
cursor: 0,
823+
tokens: tokens,
824+
filename: options.Filename,
825+
logger: NewLogger(options.EnableLogging),
826+
}
819827
}

0 commit comments

Comments
 (0)