Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.

Commit 2825708

Browse files
committed
support cleaning of feedback at user defined points
This supports transactional behaviour of the parser.
1 parent 359aa36 commit 2825708

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

base.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const (
2424

2525
// FeedbackItem is just one item of feedback.
2626
type FeedbackItem struct {
27+
Pos int
2728
Kind FeedbackKind
2829
Msg fmt.Stringer
2930
}
@@ -71,6 +72,7 @@ func (pd *ParseData) AddInfo(pos int, msg string) {
7172
pd.Result.Feedback = append(
7273
pd.Result.Feedback,
7374
&FeedbackItem{
75+
Pos: pos,
7476
Kind: FeedbackInfo,
7577
Msg: newParseMessage(pd, pos, msg),
7678
},
@@ -82,6 +84,7 @@ func (pd *ParseData) AddWarning(pos int, msg string) {
8284
pd.Result.Feedback = append(
8385
pd.Result.Feedback,
8486
&FeedbackItem{
87+
Pos: pos,
8588
Kind: FeedbackWarning,
8689
Msg: newParseMessage(pd, pos, msg),
8790
},
@@ -93,12 +96,29 @@ func (pd *ParseData) AddError(pos int, msg string, baseErr error) {
9396
pd.Result.Feedback = append(
9497
pd.Result.Feedback,
9598
&FeedbackItem{
99+
Pos: pos,
96100
Kind: FeedbackError,
97101
Msg: newParseError(pd, pos, msg, baseErr),
98102
},
99103
)
100104
}
101105

106+
// CleanFeedback returns parser errors as a single error and
107+
// additional feedback.
108+
func (pd *ParseData) CleanFeedback() {
109+
if pd.Result.HasError() || len(pd.Result.Feedback) == 0 { // in error case we need all information
110+
return
111+
}
112+
pos := pd.Result.Pos + len(pd.Result.Text) // clean until here
113+
cleanFeedback := make([]*FeedbackItem, 0, len(pd.Result.Feedback))
114+
for _, fb := range pd.Result.Feedback {
115+
if fb.Kind != FeedbackPotentialProblem || fb.Pos >= pos {
116+
cleanFeedback = append(cleanFeedback, fb)
117+
}
118+
}
119+
pd.Result.Feedback = cleanFeedback
120+
}
121+
102122
// GetFeedback returns parser errors as a single error and
103123
// additional feedback.
104124
func (pd *ParseData) GetFeedback() (string, error) {
@@ -243,7 +263,13 @@ func handleSemantics(pluginSemantics SemanticsOp, pd *ParseData, ctx interface{}
243263
func createMatchedResult(pd *ParseData, n int) {
244264
i := pd.Source.pos
245265
n += i
246-
pd.Result = &ParseResult{i, pd.Source.content[i:n], nil, -1, make([]*FeedbackItem, 0, 64)}
266+
pd.Result = &ParseResult{
267+
Pos: i,
268+
Text: pd.Source.content[i:n],
269+
Value: nil,
270+
ErrPos: -1,
271+
Feedback: make([]*FeedbackItem, 0, 64),
272+
}
247273
pd.Source.pos = n
248274
}
249275
func createUnmatchedResult(pd *ParseData, i int, msg string, baseErr error) {

0 commit comments

Comments
 (0)