Skip to content

Commit 9a7ee4a

Browse files
committed
CTagsParser is now a standard go class
This is a first refactoring that aims to create a ctags package that is not strongly tied to the builder (and so can possibly be replaced with another parser in the future) A temporary CTags command has been made to reduce the number of changes involved in this commit. It will be removed at the end of the refactoring. Signed-off-by: Cristian Maglie <[email protected]>
1 parent a07ea16 commit 9a7ee4a

File tree

5 files changed

+74
-151
lines changed

5 files changed

+74
-151
lines changed

src/arduino.cc/builder/container_add_prototypes.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (s *ContainerAddPrototypes) Run(ctx *types.Context) error {
4848
&FilterSketchSource{Source: &ctx.SourceGccMinusE},
4949
&CTagsTargetFileSaver{Source: &ctx.SourceGccMinusE, TargetFileName: constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E},
5050
&ctags.CTagsRunner{},
51-
&ctags.CTagsParser{},
51+
&ctags.CTags{},
5252
&ctags.CTagsToPrototypes{},
5353
&PrototypesAdder{},
5454
&SketchSaver{},
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ctags
2+
3+
import "arduino.cc/builder/types"
4+
5+
type CTags struct{}
6+
7+
func (c *CTags) Run(ctx *types.Context) error {
8+
parser := &CTagsParser{}
9+
ctx.CTagsOfPreprocessedSource = parser.Parse(ctx.CTagsOutput)
10+
return nil
11+
}

src/arduino.cc/builder/ctags/ctags_parser.go

+30-34
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,12 @@
3030
package ctags
3131

3232
import (
33-
"arduino.cc/builder/constants"
34-
"arduino.cc/builder/types"
3533
"bufio"
3634
"os"
37-
"reflect"
38-
"runtime"
3935
"strconv"
4036
"strings"
37+
38+
"arduino.cc/builder/types"
4139
)
4240

4341
const KIND_PROTOTYPE = "prototype"
@@ -54,32 +52,30 @@ var KNOWN_TAG_KINDS = map[string]bool{
5452
"function": true,
5553
}
5654

57-
type CTagsParser struct{}
58-
59-
func (s *CTagsParser) Run(ctx *types.Context) error {
60-
rows := strings.Split(ctx.CTagsOutput, "\n")
55+
type CTagsParser struct {
56+
tags []*types.CTag
57+
}
6158

59+
func (p *CTagsParser) Parse(ctagsOutput string) []*types.CTag {
60+
rows := strings.Split(ctagsOutput, "\n")
6261
rows = removeEmpty(rows)
6362

64-
var tags []*types.CTag
6563
for _, row := range rows {
66-
tags = append(tags, parseTag(row))
64+
p.tags = append(p.tags, parseTag(row))
6765
}
6866

69-
skipTagsWhere(tags, tagIsUnknown, ctx)
70-
skipTagsWhere(tags, tagIsUnhandled, ctx)
71-
addPrototypes(tags)
72-
removeDefinedProtypes(tags, ctx)
73-
removeDuplicate(tags)
74-
skipTagsWhere(tags, prototypeAndCodeDontMatch, ctx)
75-
76-
ctx.CTagsOfPreprocessedSource = tags
67+
p.skipTagsWhere(tagIsUnknown)
68+
p.skipTagsWhere(tagIsUnhandled)
69+
p.addPrototypes()
70+
p.removeDefinedProtypes()
71+
p.skipDuplicates()
72+
p.skipTagsWhere(prototypeAndCodeDontMatch)
7773

78-
return nil
74+
return p.tags
7975
}
8076

81-
func addPrototypes(tags []*types.CTag) {
82-
for _, tag := range tags {
77+
func (p *CTagsParser) addPrototypes() {
78+
for _, tag := range p.tags {
8379
if !tag.SkipMe {
8480
addPrototype(tag)
8581
}
@@ -108,28 +104,28 @@ func addPrototype(tag *types.CTag) {
108104
tag.PrototypeModifiers = strings.TrimSpace(tag.PrototypeModifiers)
109105
}
110106

111-
func removeDefinedProtypes(tags []*types.CTag, ctx *types.Context) {
107+
func (p *CTagsParser) removeDefinedProtypes() {
112108
definedPrototypes := make(map[string]bool)
113-
for _, tag := range tags {
109+
for _, tag := range p.tags {
114110
if tag.Kind == KIND_PROTOTYPE {
115111
definedPrototypes[tag.Prototype] = true
116112
}
117113
}
118114

119-
for _, tag := range tags {
115+
for _, tag := range p.tags {
120116
if definedPrototypes[tag.Prototype] {
121-
if ctx.DebugLevel >= 10 {
122-
ctx.GetLogger().Fprintln(os.Stdout, constants.LOG_LEVEL_DEBUG, constants.MSG_SKIPPING_TAG_ALREADY_DEFINED, tag.FunctionName)
123-
}
117+
//if ctx.DebugLevel >= 10 {
118+
// ctx.GetLogger().Fprintln(os.Stdout, constants.LOG_LEVEL_DEBUG, constants.MSG_SKIPPING_TAG_ALREADY_DEFINED, tag.FunctionName)
119+
//}
124120
tag.SkipMe = true
125121
}
126122
}
127123
}
128124

129-
func removeDuplicate(tags []*types.CTag) {
125+
func (p *CTagsParser) skipDuplicates() {
130126
definedPrototypes := make(map[string]bool)
131127

132-
for _, tag := range tags {
128+
for _, tag := range p.tags {
133129
if !definedPrototypes[tag.Prototype] && tag.SkipMe == false {
134130
definedPrototypes[tag.Prototype] = true
135131
} else {
@@ -140,13 +136,13 @@ func removeDuplicate(tags []*types.CTag) {
140136

141137
type skipFuncType func(tag *types.CTag) bool
142138

143-
func skipTagsWhere(tags []*types.CTag, skipFunc skipFuncType, ctx *types.Context) {
144-
for _, tag := range tags {
139+
func (p *CTagsParser) skipTagsWhere(skipFunc skipFuncType) {
140+
for _, tag := range p.tags {
145141
if !tag.SkipMe {
146142
skip := skipFunc(tag)
147-
if skip && ctx.DebugLevel >= 10 {
148-
ctx.GetLogger().Fprintln(os.Stdout, constants.LOG_LEVEL_DEBUG, constants.MSG_SKIPPING_TAG_WITH_REASON, tag.FunctionName, runtime.FuncForPC(reflect.ValueOf(skipFunc).Pointer()).Name())
149-
}
143+
//if skip && p.debugLevel >= 10 {
144+
// ctx.GetLogger().Fprintln(os.Stdout, constants.LOG_LEVEL_DEBUG, constants.MSG_SKIPPING_TAG_WITH_REASON, tag.FunctionName, runtime.FuncForPC(reflect.ValueOf(skipFunc).Pointer()).Name())
145+
//}
150146
tag.SkipMe = skip
151147
}
152148
}

0 commit comments

Comments
 (0)