| 1 |
package parser |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"sort" |
| 6 |
|
| 7 |
"github.com/robertkrimen/otto/file" |
| 8 |
"github.com/robertkrimen/otto/token" |
| 9 |
) |
| 10 |
|
| 11 |
const ( |
| 12 |
errUnexpectedToken = "Unexpected token %v" |
| 13 |
errUnexpectedEndOfInput = "Unexpected end of input" |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
type Error struct { |
| 52 |
Message string |
| 53 |
Position file.Position |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
func (e Error) Error() string { |
| 59 |
filename := e.Position.Filename |
| 60 |
if filename == "" { |
| 61 |
filename = "(anonymous)" |
| 62 |
} |
| 63 |
return fmt.Sprintf("%s: Line %d:%d %s", |
| 64 |
filename, |
| 65 |
e.Position.Line, |
| 66 |
e.Position.Column, |
| 67 |
e.Message, |
| 68 |
) |
| 69 |
} |
| 70 |
|
| 71 |
func (p *parser) error(place interface{}, msg string, msgValues ...interface{}) { |
| 72 |
var idx file.Idx |
| 73 |
switch place := place.(type) { |
| 74 |
case int: |
| 75 |
idx = p.idxOf(place) |
| 76 |
case file.Idx: |
| 77 |
if place == 0 { |
| 78 |
idx = p.idxOf(p.chrOffset) |
| 79 |
} else { |
| 80 |
idx = place |
| 81 |
} |
| 82 |
default: |
| 83 |
panic(fmt.Errorf("error(%T, ...)", place)) |
| 84 |
} |
| 85 |
|
| 86 |
position := p.position(idx) |
| 87 |
msg = fmt.Sprintf(msg, msgValues...) |
| 88 |
p.errors.Add(position, msg) |
| 89 |
} |
| 90 |
|
| 91 |
func (p *parser) errorUnexpected(idx file.Idx, chr rune) { |
| 92 |
if chr == -1 { |
| 93 |
p.error(idx, errUnexpectedEndOfInput) |
| 94 |
return |
| 95 |
} |
| 96 |
p.error(idx, errUnexpectedToken, token.ILLEGAL) |
| 97 |
} |
| 98 |
|
| 99 |
func (p *parser) errorUnexpectedToken(tkn token.Token) { |
| 100 |
if tkn == token.EOF { |
| 101 |
p.error(file.Idx(0), errUnexpectedEndOfInput) |
| 102 |
return |
| 103 |
} |
| 104 |
value := tkn.String() |
| 105 |
switch tkn { |
| 106 |
case token.BOOLEAN, token.NULL: |
| 107 |
p.error(p.idx, errUnexpectedToken, p.literal) |
| 108 |
case token.IDENTIFIER: |
| 109 |
p.error(p.idx, "Unexpected identifier") |
| 110 |
case token.KEYWORD: |
| 111 |
|
| 112 |
p.error(p.idx, "Unexpected reserved word") |
| 113 |
case token.NUMBER: |
| 114 |
p.error(p.idx, "Unexpected number") |
| 115 |
case token.STRING: |
| 116 |
p.error(p.idx, "Unexpected string") |
| 117 |
default: |
| 118 |
p.error(p.idx, errUnexpectedToken, value) |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
type ErrorList []*Error |
| 124 |
|
| 125 |
|
| 126 |
func (el *ErrorList) Add(position file.Position, msg string) { |
| 127 |
*el = append(*el, &Error{Position: position, Message: msg}) |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
func (el *ErrorList) Reset() { |
| 132 |
*el = (*el)[0:0] |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
func (el *ErrorList) Len() int { |
| 137 |
return len(*el) |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
func (el *ErrorList) Swap(i, j int) { |
| 142 |
(*el)[i], (*el)[j] = (*el)[j], (*el)[i] |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
func (el *ErrorList) Less(i, j int) bool { |
| 147 |
x := (*el)[i].Position |
| 148 |
y := (*el)[j].Position |
| 149 |
if x.Filename < y.Filename { |
| 150 |
return true |
| 151 |
} |
| 152 |
if x.Filename == y.Filename { |
| 153 |
if x.Line < y.Line { |
| 154 |
return true |
| 155 |
} |
| 156 |
if x.Line == y.Line { |
| 157 |
return x.Column < y.Column |
| 158 |
} |
| 159 |
} |
| 160 |
return false |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
func (el *ErrorList) Sort() { |
| 165 |
sort.Sort(el) |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
func (el *ErrorList) Error() string { |
| 170 |
switch len(*el) { |
| 171 |
case 0: |
| 172 |
return "no errors" |
| 173 |
case 1: |
| 174 |
return (*el)[0].Error() |
| 175 |
default: |
| 176 |
return fmt.Sprintf("%s (and %d more errors)", (*el)[0].Error(), len(*el)-1) |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
func (el *ErrorList) Err() error { |
| 183 |
if len(*el) == 0 { |
| 184 |
return nil |
| 185 |
} |
| 186 |
return el |
| 187 |
} |