beginning of a dns lexer

This commit is contained in:
Miek Gieben 2011-01-16 15:30:59 +01:00
parent 5cad281ea4
commit 0c6b29aabd
1 changed files with 97 additions and 0 deletions

97
dns.y
View File

@ -58,3 +58,100 @@ func (DnsLex) Lex(yylval *yySymType) int {
return 0
}
func isSeparator(c byte) bool {
switch c {
case '(', ')', '<', '>', '@', ',', ';', ':', '\\', '"', '/', '[', ']', '?', '=', '{', '}', ' ', '\t':
return true
}
return false
}
func isSpace(c byte) bool {
switch c {
case ' ', '\t', '\r', '\n':
return true
}
return false
}
func isCtl(c byte) bool { return (0 <= c && c <= 31) || c == 127 }
func isChar(c byte) bool { return 0 <= c && c <= 127 }
func isAnyText(c byte) bool { return !isCtl(c) }
func isQdText(c byte) bool { return isAnyText(c) && c != '"' }
func isToken(c byte) bool { return isChar(c) && !isCtl(c) && !isSeparator(c) }
// This is a best effort parse, so errors are not returned, instead not all of
// the input string might be parsed. result is always non-nil.
// must never begin with a string
func scan(s string) (string, int) {
if len(s) == 0 {
println("a bit short")
}
raw := []byte(s)
chunk := ""
off := 0
brace := 0
redo:
for off < len(raw) {
c := raw[off]
// println(c, string(c))
switch c {
case '\n':
// normal case??
if brace > 0 {
off++
continue
}
case '.':
// println("off", off)
if off == 0 {
print("DOT")
return ".", off + 1
} else {
return chunk, off
}
case ' ','\t':
if brace != 0 {
off++
continue
}
// eat whitespace
// Look at next char
if raw[off+1] == ' ' {
off++
continue
} else {
// if chunk is empty, we have skipped whitespace, and seen nothing
if len(chunk) == 0 {
off++
goto redo
}
print("VAL ")
return chunk, off
}
case '(':
brace++
off++
continue
case ')':
brace--
if brace < 0 {
println("syntax error")
}
off++
continue
}
if c == ' ' { println("adding space") }
if c == '\t' { println("adding tab") }
chunk += string(c)
off++
}
print("VAL ")
return chunk, off
}