1 2 3 4 5 6 7 8 9 10 11 12 13 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
package rfcparser
import (
"bufio"
"errors"
"fmt"
"io"
)
type TokenType int
const (
TokenTypeEOF TokenType = iota
TokenTypeError
TokenTypeSP
TokenTypeExclamation
TokenTypeDQuote
TokenTypeHash
TokenTypeDollar
TokenTypePercent
TokenTypeAmpersand
TokenTypeSQuote
TokenTypeLParen
TokenTypeRParen
TokenTypeAsterisk
TokenTypePlus
TokenTypeComma
TokenTypeMinus
TokenTypePeriod
TokenTypeSlash
TokenTypeSemicolon
TokenTypeColon
TokenTypeLess
TokenTypeEqual
TokenTypeGreater
TokenTypeQuestion
TokenTypeAt
TokenTypeLBracket
TokenTypeRBracket
TokenTypeCaret
TokenTypeUnderscore
TokenTyeBacktick
TokenTypeLCurly
TokenTypePipe
TokenTypeRCurly
TokenTypeTilde
TokenTypeBackslash
TokenTypeDigit
TokenTypeChar
TokenTypeExtendedChar
TokenTypeCR
TokenTypeLF
TokenTypeCTL
TokenTypeTab
TokenTypeDelete
TokenTypeZero
)
type Token struct {
TType TokenType
Value byte
Offset int
}
type Scanner struct {
source Reader
currentByte byte
offset int
}
type Reader interface {
io.Reader
ReadByte() (byte, error)
ReadBytes(byte) ([]byte, error)
}
func NewScanner(source io.Reader) *Scanner {
return &Scanner{
source: bufio.NewReader(source),
}
}
func NewScannerWithReader(source Reader) *Scanner {
return &Scanner{
source: source,
}
}
func (s *Scanner) ConsumeBytes(dst []byte) error {
// We have already read a byte at this point, so we need to
// skip this one.
dst[0] = s.currentByte
if _, err := io.ReadFull(s.source, dst[1:]); err != nil {
if errors.Is(err, io.ErrUnexpectedEOF) {
return io.EOF
}
return err
}
s.offset += len(dst) - 1
return nil
}
func (s *Scanner) ConsumeUntilNewLine() ([]byte, error) {
return s.source.ReadBytes('\n')
}
func (s *Scanner) ScanToken() (Token, error) {
b, err := s.advance()
if err != nil {
if errors.Is(err, io.EOF) {
return s.makeEOF(), nil
}
return Token{}, nil
}
if isByteDigit(b) {
return s.makeToken(TokenTypeDigit), nil
}
if isByteAlpha(b) {
return s.makeToken(TokenTypeChar), nil
}
if isByteExtendedChar(b) {
return s.makeToken(TokenTypeExtendedChar), nil
}
if isByteCTL(b) {
switch b {
case 0x0:
return s.makeToken(TokenTypeZero), nil
case '\r':
return s.makeToken(TokenTypeCR), nil
case '\n':
return s.makeToken(TokenTypeLF), nil
case '\t':
return s.makeToken(TokenTypeTab), nil
default:
return s.makeToken(TokenTypeCTL), nil
}
}
switch b {
case ' ':
return s.makeToken(TokenTypeSP), nil
case '!':
return s.makeToken(TokenTypeExclamation), nil
case '"':
return s.makeToken(TokenTypeDQuote), nil
case '#':
return s.makeToken(TokenTypeHash), nil
case '$':
return s.makeToken(TokenTypeDollar), nil
case '%':
return s.makeToken(TokenTypePercent), nil
case '&':
return s.makeToken(TokenTypeAmpersand), nil
case '\'':
return s.makeToken(TokenTypeSQuote), nil
case '\\':
return s.makeToken(TokenTypeBackslash), nil
case '(':
return s.makeToken(TokenTypeLParen), nil
case ')':
return s.makeToken(TokenTypeRParen), nil
case '*':
return s.makeToken(TokenTypeAsterisk), nil
case '+':
return s.makeToken(TokenTypePlus), nil
case ',':
return s.makeToken(TokenTypeComma), nil
case '-':
return s.makeToken(TokenTypeMinus), nil
case '.':
return s.makeToken(TokenTypePeriod), nil
case '/':
return s.makeToken(TokenTypeSlash), nil
case ':':
return s.makeToken(TokenTypeColon), nil
case ';':
return s.makeToken(TokenTypeSemicolon), nil
case '<':
return s.makeToken(TokenTypeLess), nil
case '=':
return s.makeToken(TokenTypeEqual), nil
case '>':
return s.makeToken(TokenTypeGreater), nil
case '?':
return s.makeToken(TokenTypeQuestion), nil
case '@':
return s.makeToken(TokenTypeAt), nil
case '[':
return s.makeToken(TokenTypeLBracket), nil
case ']':
return s.makeToken(TokenTypeRBracket), nil
case '^':
return s.makeToken(TokenTypeCaret), nil
case '_':
return s.makeToken(TokenTypeUnderscore), nil
case '`':
return s.makeToken(TokenTyeBacktick), nil
case '{':
return s.makeToken(TokenTypeLCurly), nil
case '|':
return s.makeToken(TokenTypePipe), nil
case '}':
return s.makeToken(TokenTypeRCurly), nil
case '~':
return s.makeToken(TokenTypeTilde), nil
case 0x7F:
return s.makeToken(TokenTypeDelete), nil
}
return Token{}, fmt.Errorf("unexpected character %v", b)
}
func (s *Scanner) ResetOffsetCounter() {
s.offset = 0
}
func (s *Scanner) advance() (byte, error) {
b, err := s.source.ReadByte()
if err != nil {
return 0, err
}
s.currentByte = b
s.offset += 1
return b, nil
}
func (s *Scanner) makeToken(t TokenType) Token {
return Token{
TType: t,
Value: s.currentByte,
Offset: s.offset,
}
}
func (s *Scanner) makeEOF() Token {
return Token{
TType: TokenTypeEOF,
Value: 0,
Offset: s.offset,
}
}
func isByteAlpha(b byte) bool {
return (b >= 65 && b <= 90) || (b >= 97 && b <= 122)
}
func isByteDigit(b byte) bool {
return b >= byte('0') && b <= byte('9')
}
func isByteExtendedChar(b byte) bool {
return b >= 128
}
func isByteCTL(b byte) bool {
return b <= 31
}
func ByteToLower(b byte) byte {
if b >= 65 && b <= 90 {
return 97 + (b - byte(65))
}
return b
}
|