@@ -3,6 +3,7 @@ package lexmachine
33import (
44 "fmt"
55 "strconv"
6+ "strings"
67 "testing"
78
89 "github.com/timtadh/data-structures/test"
@@ -292,3 +293,90 @@ func TestRegression(t *testing.T) {
292293
293294 }
294295}
296+
297+ func TestRegression2 (t * testing.T ) {
298+
299+ text := `# dhcpd.conf
300+ #
301+ # Sample configuration file for ISC dhcpd
302+ #
303+
304+ # option definitions common to all supported networks...
305+ option domain-name "example.org";
306+ option domain-name-servers ns1.example.org, ns2.example.org;
307+
308+ default-lease-time 600;
309+ max-lease-time 7200;
310+
311+ # The ddns-updates-style parameter controls whether or not the server will
312+ # attempt to do a DNS update when a lease is confirmed. We default to the
313+ # behavior of the version 2 packages ('none', since DHCP v2 didn't
314+ # have support for DDNS.)
315+ ddns-update-style none;
316+
317+ # If this DHCP server is the official DHCP server for the local
318+ # network, the authoritative directive should be uncommented.
319+ #authoritative;
320+ `
321+
322+ literals := []string {
323+ "{" ,
324+ "}" ,
325+ ";" ,
326+ "," ,
327+ }
328+ tokens := []string {
329+ "COMMENT" ,
330+ "ID" ,
331+ }
332+ tokens = append (tokens , literals ... )
333+ tokenIds := map [string ]int {}
334+ for i , tok := range tokens {
335+ tokenIds [tok ] = i
336+ }
337+ newLexer := func () * Lexer {
338+ lex := NewLexer ()
339+
340+ skip := func (* Scanner , * machines.Match ) (interface {}, error ) {
341+ return nil , nil
342+ }
343+ token := func (name string ) Action {
344+ return func (s * Scanner , m * machines.Match ) (interface {}, error ) {
345+ return s .Token (tokenIds [name ], string (m .Bytes ), m ), nil
346+ }
347+ }
348+
349+ lex .Add ([]byte (`#[^\n]*\n?` ), token ("COMMENT" ))
350+ lex .Add ([]byte (`([a-z]|[A-Z]|[0-9]|_|\-|\.)*` ), token ("ID" ))
351+ lex .Add ([]byte (`"([^\\"]|(\\.))*"` ), token ("ID" ))
352+ lex .Add ([]byte ("[\n \t ]" ), skip )
353+ for _ , lit := range literals {
354+ lex .Add ([]byte (lit ), token (lit ))
355+ }
356+
357+ err := lex .Compile ()
358+ if err != nil {
359+ panic (err )
360+ }
361+
362+ return lex
363+ }
364+
365+ scanner , err := newLexer ().Scanner ([]byte (text ))
366+ if err != nil {
367+ return
368+ }
369+ for tok , err , eof := scanner .Next (); ! eof ; tok , err , eof = scanner .Next () {
370+ if err != nil {
371+ t .Error (err )
372+ }
373+ token := tok .(* Token )
374+ fmt .Printf ("%-7v | %-10v | %v:%v-%v:%v\n " ,
375+ tokens [token .Type ],
376+ strings .TrimSpace (string (token .Lexeme )),
377+ token .StartLine ,
378+ token .StartColumn ,
379+ token .EndLine ,
380+ token .EndColumn )
381+ }
382+ }
0 commit comments