Skip to content
Browse files

cc: Use strutil.Dict

  • Loading branch information...
1 parent 8348e68 commit 5cd99feca29b0304d44e3898b406951e5714637f @cznic committed Feb 10, 2013
Showing with 28 additions and 10 deletions.
  1. +7 −2 cc/all_test.go
  2. +21 −8 cc/cc.l
View
9 cc/all_test.go
@@ -12,6 +12,8 @@ import (
"io"
"strings"
"testing"
+
+ "github.com/cznic/strutil"
)
func TestScanPeekNext(t *testing.T) {
@@ -24,10 +26,11 @@ func TestScanPeekNext(t *testing.T) {
for n := 0; n <= N; n++ {
b = b[:n]
for sz := 0; sz <= len(b)+2; sz++ {
- //r := bytes.NewReader(b)
r := rf()
+ d := strutil.NewDict()
failed := false
l := newLexer(
+ d,
"file",
r,
func(file string, line, col int, msg string, args ...interface{}) bool {
@@ -225,7 +228,9 @@ func TestLineDirectives(t *testing.T) {
}
for i, test := range tab {
r := strings.NewReader(test.s)
+ d := strutil.NewDict()
l := newLexer(
+ d,
m,
r,
func(file string, line, col int, msg string, args ...interface{}) bool {
@@ -245,7 +250,7 @@ func TestLineDirectives(t *testing.T) {
}
}
- if l.file != test.file || l.line != test.line || l.col != test.col {
+ if l.file != d.Id(test.file) || l.line != test.line || l.col != test.col {
t.Fatalf(
`%d: (%q) got "%s.%d:%d", exp "%s.%d:%d"`,
i, test.s,
View
29 cc/cc.l
@@ -44,31 +44,36 @@ import (
"fmt"
"io"
"strconv"
+
+ "github.com/cznic/strutil"
)
// Errf is an error reporting function. If it returns false then further
// procesing (scanning, parsing, compiling) should be aborted.
type Errf func(file string, line, col int, msg string, args ...interface{}) bool
type lexer struct {
- file string
+ dict *strutil.Dict
+ file int
line int
col int
r io.Reader
buf []byte
token []byte
+ id int
errf Errf
err error
prev int
}
// newLexer returns a new `lexer`. `buf` is the scanner buffer to use, which
// may be nil.
-func newLexer(file string, r io.Reader, errf Errf, buf []byte) (l *lexer) {
+func newLexer(dict *strutil.Dict, file string, r io.Reader, errf Errf, buf []byte) (l *lexer) {
const bufSize = 1<<16
l = &lexer{
- file: file,
+ dict: dict,
+ file: dict.Id(file),
line: 1,
col: 1,
r: r,
@@ -82,8 +87,16 @@ func newLexer(file string, r io.Reader, errf Errf, buf []byte) (l *lexer) {
return
}
+func (l *lexer) s(id int) (str string) {
+ var ok bool
+ if str, ok = l.dict.S(id); !ok {
+ str = "<MISSING:string from id>"
+ }
+ return
+}
+
func (l *lexer) error(msg string, args ...interface{}) {
- if l.errf(l.file, l.line, l.col, msg, args...) {
+ if l.errf(l.s(l.file), l.line, l.col, msg, args...) {
l.err = io.EOF
}
}
@@ -197,7 +210,7 @@ LW [ \t\v\f]
<FILE>.|\n sc = EOL
<FN>[^\n\"]*\"
- l.file = string(l.token[:len(l.token)-1])
+ l.file = l.dict.Id(string(l.token[:len(l.token)-1]))
l.line = line - 1
sc = EOL
@@ -305,7 +318,7 @@ L?\"(\\.|[^\\"])*\" // { count(); return(STRING_LITERAL); }
%%
panic(fmt.Errorf(
- "%s.%d:%d: unreachable, sc %d, l.peek() == %d",
- l.file, l.line, l.col, sc, l.peek()),
- )
+ "%s.%d:%d: unreachable, sc %d, l.peek() %d, l.token %q",
+ l.file, l.line, l.col, sc, l.peek(), l.token,
+ ))
}

0 comments on commit 5cd99fe

Please sign in to comment.
Something went wrong with that request. Please try again.