Exemplo n.º 1
0
Arquivo: rxq.go Projeto: proebsting/re
func main() {
	var ifile *bufio.Scanner
	if len(os.Args) == 2 {
		ifile = rx.MkScanner("-")
	} else if len(os.Args) == 3 {
		ifile = rx.MkScanner(os.Args[2])
	} else {
		log.Fatal("usage: rxq \"rexpr\" [file]")
	}
	spec := os.Args[1]
	fmt.Printf("regexp: %s\n", spec)
	dfa, err := rx.Compile(spec)
	if err != nil {
		log.Fatal(err)
	}

	// load and process candidate strings
	for i := 0; ifile.Scan(); i++ {
		s := ifile.Text()
		if dfa.Accepts(s) != nil {
			fmt.Println("accept:", s)
		} else {
			fmt.Println("REJECT:", s)
		}
	}
	rx.CkErr(ifile.Err())
}
Exemplo n.º 2
0
// setup performs global initialization actions
func setup() {
	rxsys.Interval() // initialize timestamps
	options()        // process command-line options
	if verbose {
		showopts() // show command options
	}

	// handle -P (profiling) option
	pfname := *val['P'] // profiling filename
	if pfname != "" {   // if set
		pfile, err := os.Create(pfname)
		rx.CkErr(err)
		pprof.StartCPUProfile(pfile)
	}

	// set up input scanning
	if *val['e'] != "" { // if -e specified
		if len(flag.Args()) > 0 {
			log.Fatal("-e option precludes reading " + flag.Arg(0))
		}
		input = bufio.NewScanner(bytes.NewReader([]byte(*val['e'])))
	} else {
		input = rx.MkScanner(rx.OneInputFile())
	}

	timestamp("init")
}
Exemplo n.º 3
0
Arquivo: rxx.go Projeto: proebsting/re
func main() {
	args := os.Args
	if len(args) != 3 {
		log.Fatal("usage: rxx efile sfile")
	}
	ename := args[1]
	sfile := rx.MkScanner(args[2])

	labels :=
		"123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
	elist := make([]tester, 0, len(labels))

	// load and compile regexps
	fmt.Println()
	tlist := make([]rx.Node, 0) // list of valid parse trees
	rx.LoadExpressions(ename, func(x *rx.RegExParsed) {
		spec := x.Expr
		ptree := x.Tree
		err := x.Err
		if err != nil {
			fmt.Printf("ERR %s\n", spec)
			elist = append(elist, tester{" ", spec, nil, 0})
		} else if ptree == nil {
			fmt.Printf("    %s\n", spec)
			elist = append(elist, tester{" ", spec, nil, 0})
		} else {
			i := len(tlist)
			if i >= len(labels) {
				log.Fatal("too many regular expressions")
			}
			label := string(labels[i : i+1])
			fmt.Printf("%s:  %s\n", label, spec)
			atree := rx.Augment(ptree, len(tlist))
			tlist = append(tlist, atree)
			elist = append(elist, tester{label, spec, ptree, i})
		}
	})

	dfa := rx.MultiDFA(tlist)
	_ = dfa.Minimize()   // should have no effect
	_ = dfa.Minimize()   // should again have no effect
	dfa = dfa.Minimize() // not necessary, but a good stress test
	dfa = dfa.Minimize() // especially if done more than once

	// read and test candidate strings
	fmt.Println()
	for sfile.Scan() {
		s := string(sfile.Bytes())
		results := dfa.Accepts(s)
		if results == nil {
			results = &rx.BitSet{}
		}
		for _, e := range elist {
			if e.tree == nil {
				fmt.Print(" ")
			} else {
				if results.Test(e.index) {
					fmt.Print(e.label)
				} else {
					fmt.Print("-")
				}
			}
		}
		fmt.Printf("  %s\n", s)
	}
	rx.CkErr(sfile.Err())
}