Пример #1
0
func (lex *Lexie) NewReadFile(path string) {
	lex.Im = in.ImReadFile(path)

	lex.NFA_Machine = make([]*nfa.NFA_PoolType, 0, 100)
	lex.DFA_Machine = make([]*DFA_PoolType, 0, 100)

	// vv=in.ImDefinedValueType {Seq:1 WhoAmI:ReservedWords NameValueStr:map[and:Tok_L_AND not:Tok_not as:Tok_as in:Tok_in bor:Tok_B_OR band:Tok_B_AND xor:Tok_XOR or:Tok_L_OR true:Tok_true false:Tok_false export:Tok_export] NameValue:map[and:4 true:32 as:34 bor:42 band:41 xor:64 or:5 false:33 not:31 export:35 in:28] Reverse:map[5:or 32:true 42:bor 31:not 41:band 35:export 33:false 28:in 64:xor 4:and 34:as] SeenAt:map[bor:{LineNo:[39] FileName:[unk-file]} band:{LineNo:[39] FileName:[unk-file]} and:{LineNo:[39] FileName:[unk-file]} true:{LineNo:[39] FileName:[unk-file]} export:{LineNo:[39] FileName:[unk-file]} in:{LineNo:[39] FileName:[unk-file]} as:{LineNo:[39] FileName:[unk-file]} or:{LineNo:[39] FileName:[unk-file]} false:{LineNo:[39] FileName:[unk-file]} not:{LineNo:[39 39] FileName:[unk-file unk-file]} xor:{LineNo:[39] FileName:[unk-file]}]}, File: /Users/corwin/Projects/pongo2/lexie/dfa/match.go LineNo:260
	for ii, vv := range lex.Im.Def.DefsAre {
		// ["ReservedWords"] {
		// func (st *SymbolTable) DefineReservedWord(name string, fxid int) (ss *SymbolType) {
		_ = ii
		_ = vv
		com.DbPrintf("dfa5", "vv=%T %+v, %s\n", vv, vv, com.LF())
	}

	for ii, vv := range lex.Im.Machine {
		nm := vv.Name

		Nfa := nfa.NewNFA_Pool()
		Cur := Nfa.GetNFA()
		Nfa.InitState = Cur
		for jj, ww := range vv.Rules {
			rVx := ww.Rv
			if ww.ReservedWord {
				com.DbPrintf("dfa5", "This rule rv=%d is a reserved word rule, AAbbCC\n", rVx)
			}
			ww_A := convRuleToActionFlag(ww)
			if ww.Repl {
				rVx = 9900 // 9900 is replace
				com.DbPrintf("match", "###################################### ww.Replace: ii=%d jj=%d ->%s<-, %s\n", ii, jj, ww.ReplString, com.LF())
			}
			cur := -1
			if ww.PatternType == 2 {
				com.DbPrintf("db_Matcher_02", "ADDING AT %2d RE: %-30s (Rv:%2d, final=%4d), %s\n", jj, "<M_EOF>", ww.Rv, rVx, com.LF())
				cur = Nfa.AddReInfo(re.X_EOF, "", jj+1, rVx, nfa.InfoType{Action: ww_A, NextState: ww.Call})
			} else {
				com.DbPrintf("db_Matcher_02", "M= ->%s<- Adding at %2d RE: %-30s (Rv:%2d, final=%4d), %s\n", ww.Pattern, jj, ww.Pattern, ww.Rv, rVx, com.LF())
				cur = Nfa.AddReInfo(ww.Pattern, "", jj+1, rVx, nfa.InfoType{Action: ww_A, NextState: ww.Call, ReplStr: ww.ReplString})
			}
			if ww.ReservedWord {
				Nfa.SetReservedWord(cur)
			}
		}

		com.DbPrintf("match", "BuildDFA_2: Nfa.Sigma Before Finialize->%s<-\n", Nfa.Sigma)
		if com.DbOn("db_Matcher_02") {
			com.DbPrintf("match", "NFA for (Before Finialize) ->%s<-\n", nm)
			Nfa.DumpPool(false)
		}

		Nfa.FinializeNFA()

		com.DbPrintf("match", "BuildDFA_2: Nfa.Sigma ->%s<-\n", Nfa.Sigma)
		if com.DbOn("db_Matcher_02") {
			com.DbPrintf("match", "Final NFA for ->%s<-\n", nm)
			Nfa.DumpPool(false)
		}
		lex.NFA_Machine = append(lex.NFA_Machine, Nfa)

		Dfa := NewDFA_Pool()
		Dfa.ConvNDA_to_DFA(Nfa)
		if com.DbOn("db_Matcher_02") {
			com.DbPrintf("match", "Final DFA for ->%s<-\n", nm)
			Dfa.DumpPool(false)
		}
		lex.DFA_Machine = append(lex.DFA_Machine, Dfa)

		if com.DbOn("db_Matcher_02") {

			last := len(lex.DFA_Machine) - 1

			newFile := fmt.Sprintf("../ref/mmm_%s_%d.tst", "machine", last)
			gvFile := fmt.Sprintf("../ref/mmm_%s_%d.gv", "machine", last)
			svgFile := fmt.Sprintf("../ref/mmm_%s_%d.svg", "machine", last)

			fp, _ := com.Fopen(newFile, "w")
			lex.DFA_Machine[last].DumpPoolJSON(fp, fmt.Sprintf("Lex-Machine-%d", last), 1)
			fp.Close()

			gv, _ := com.Fopen(gvFile, "w")
			lex.DFA_Machine[last].GenerateGVFile(gv, fmt.Sprintf("Lex-Machine-%d", last), 1)
			gv.Close()

			out, err := exec.Command("/usr/local/bin/dot", "-Tsvg", "-o"+svgFile, gvFile).Output()
			if err != nil {
				com.DbPrintf("match", "%sError%s from dot, %s, %s\n", com.Red, com.Reset, err, com.LF())
				com.DbPrintf("match", "Output: %s\n", out)
			}
		}
	}
}
Пример #2
0
func (s *NFA_to_DFA_TestSuite) TestLexie(c *C) {

	return
	fmt.Fprintf(os.Stderr, "Test NFA to DFA, %s\n", tr.LF())

	n_err := 0
	n_skip := 0

	dbOn["db_DFAGen"] = true
	dbOn["db_DumpDFAPool"] = true
	dbOn["db_DFA_LnNo"] = true

	// -----------------------------------------------------------------------------------------------------------------
	// -----------------------------------------------------------------------------------------------------------------
	// -----------------------------------------------------------------------------------------------------------------
	// vv := Lexie01Data[11]
	for ii, vv := range Lexie01Data {
		if !vv.SkipTest {
			// {Test: "0011", Re: `a(bcd)*(ghi)+(jkl)*X`, Rv: 1011},     //
			Nfa := nfa.NewNFA_Pool()
			Cur := Nfa.GetNFA()
			Nfa.InitState = Cur

			c.Log("\n\n--- %d NFA to DFA Test: %s -----------------------------------------------------------------------------\n\n", ii, vv.Test)
			fmt.Printf("\n\n--- %d NFA to DFA Test: %s -----------------------------------------------------------------------------\n\n", ii, vv.Test)
			newFile := fmt.Sprintf("./ref/dfa_%s.tst", vv.Test)
			cmpFile := fmt.Sprintf("./ref/dfa_%s.ref", vv.Test)
			gvFile := fmt.Sprintf("./ref/dfa_%s.gv", vv.Test)
			svgFile := fmt.Sprintf("./ref/dfa_%s.svg", vv.Test)
			tabFile := fmt.Sprintf("./ref/dfa_%s.tab", vv.Test)
			Nfa.AddReInfo(vv.Re, "", 1, vv.Rv, nfa.InfoType{})
			// Nfa.Sigma = Nfa.GenerateSigma()
			Nfa.Sigma = Nfa.GenerateSigma()
			fmt.Printf("\nSigma: ->%s<-\n", Nfa.Sigma)
			fmt.Printf("\n\nRe: %s\n", vv.Re)
			Nfa.DumpPool(false)

			Dfa := dfa.NewDFA_Pool()
			Dfa.ConvNDA_to_DFA(Nfa)

			fp, _ := Fopen(newFile, "w")
			Dfa.DumpPoolJSON(fp, vv.Re, vv.Rv)
			fp.Close()
			newData, err := ioutil.ReadFile(newFile)
			if err != nil {
				panic("unable to read file, " + cmpFile)
			}

			if sizlib.Exists(cmpFile) {
				ref, err := ioutil.ReadFile(cmpFile)
				if err != nil {
					panic("unable to read file, " + cmpFile)
				}
				if string(ref) != string(newData) {
					c.Check(string(newData), Equals, string(ref))
					fmt.Printf("%sError%s: Test case %s failed to match\n", Red, Reset, vv.Test)
					n_err++
				}
			} else {
				n_skip++
			}
			// -----------------------------------------------------------------------------------------------------------------
			// Post dump
			// -----------------------------------------------------------------------------------------------------------------
			fmt.Printf("----------------------------------------------------------------------------------------------------\n")
			fmt.Printf("Bottom .. machine is...\n")
			fmt.Printf("----------------------------------------------------------------------------------------------------\n")
			Dfa.DumpPool(false)

			// func (dfa *NFA_PoolType) GenerateGVFile(fo io.Writer, td string, tn int) {
			gv, _ := Fopen(gvFile, "w")
			Dfa.GenerateGVFile(gv, vv.Re, vv.Rv)
			gv.Close()

			tf, _ := Fopen(tabFile, "w")
			//Dfa.DumpPoolJSON(fp, vv.Re, vv.Rv)
			Dfa.OutputInFormat(tf, "text") // text, go-code, c-code, json, xml etc.
			tf.Close()

			out, err = exec.Command("/usr/local/bin/dot", "-Tsvg", "-o"+svgFile, gvFile).Output()
			if err != nil {
				fmt.Printf("Error from dot, %s, %s\n", err, tr.LF())
				fmt.Printf("Output: %s\n", out)
			}
		}
	}
	// -----------------------------------------------------------------------------------------------------------------
	// -----------------------------------------------------------------------------------------------------------------
	// -----------------------------------------------------------------------------------------------------------------

	if n_err > 0 {
		fmt.Fprintf(os.Stderr, "%sFailed, # of errors = %d%s\n", Red, n_err, Reset)
		DbPrintf("debug", "\n\n%sFailed, # of errors = %d%s\n", Red, n_err, Reset)
	} else {
		fmt.Fprintf(os.Stderr, "%sPASS%s\n", Green, Reset)
		DbPrintf("debug", "\n\n%sPASS%s\n", Green, Reset)
	}
}
Пример #3
0
func (s *LexieTestSuite) TestLexie(c *C) {

	return
	fmt.Fprintf(os.Stderr, "Test Parsing of REs, %s\n", tr.LF())

	dbOn["db_NFA"] = true
	dbOn["db_NFA_LnNo"] = true
	dbOn["db_DumpPool"] = true
	dbOn["parseExpression"] = true
	dbOn["CalcLength"] = true

	// Add a test for any issue
	c.Check(42, Equals, 42)
	// c.Assert("nope", Matches, "hel.*there")
	fmt.Printf("**** In Test Issues\n")
	//x := test7GenDFA()
	//c.Check(x, Equals, 0)

	n_err := 0
	n_skip := 0

	for ii, vv := range Lexie01Data {
		fmt.Printf("\n\n--- %d Test: %s -----------------------------------------------------------------------------\n\n", ii, vv.Test)

		Pool := nfa.NewNFA_Pool()
		Cur := Pool.GetNFA()
		Pool.InitState = Cur

		Pool.AddReInfo(vv.Re, "", 1, vv.Rv, nfa.InfoType{})
		Pool.Sigma = Pool.GenerateSigma()

		if false {
			DbPrintf("test7", "Pool=%s\n", tr.SVarI(Pool))
		}
		Pool.DumpPool(false)
		Pool.DumpPoolJSON(os.Stdout, vv.Re, vv.Rv)

		fmt.Printf("Sigma: ->%s<-\n", Pool.Sigma)

		newFile := fmt.Sprintf("./ref/nfa_%s.tst", vv.Test)
		cmpFile := fmt.Sprintf("./ref/nfa_%s.ref", vv.Test)
		gvFile := fmt.Sprintf("./ref/nfa_%s.gv", vv.Test)
		svgFile := fmt.Sprintf("./ref/nfa_%s.svg", vv.Test)
		fp, _ := Fopen(newFile, "w")
		Pool.DumpPoolJSON(fp, vv.Re, vv.Rv)
		fp.Close()
		newData, err := ioutil.ReadFile(newFile)
		if err != nil {
			panic("unable to read file, " + cmpFile)
		}

		if sizlib.Exists(cmpFile) {
			ref, err := ioutil.ReadFile(cmpFile)
			if err != nil {
				panic("unable to read file, " + cmpFile)
			}
			if string(ref) != string(newData) {
				c.Check(string(newData), Equals, string(ref))
				fmt.Printf("%sError%s: Test case %s failed to match\n", Red, Reset, vv.Test)
				n_err++
			}
		} else {
			n_skip++
		}

		gv, _ := Fopen(gvFile, "w")
		Pool.GenerateGVFile(gv, vv.Re, vv.Rv)
		gv.Close()

		out, err = exec.Command("/usr/local/bin/dot", "-Tsvg", "-o"+svgFile, gvFile).Output()
		if err != nil {
			fmt.Printf("Error from dot, %s, %s\n", err, tr.LF())
			fmt.Printf("Output: %s\n", out)
		}
	}
	if n_skip > 0 {
		fmt.Fprintf(os.Stderr, "%sSkipped, # of files without automated checks = %d%s\n", Yellow, n_skip, Reset)
		DbPrintf("debug", "\n\n%sSkipped, # of files without automated checks = %d%s\n", Yellow, n_skip, Reset)
	}
	if n_err > 0 {
		fmt.Fprintf(os.Stderr, "%sFailed, # of errors = %d%s\n", Red, n_err, Reset)
		DbPrintf("debug", "\n\n%sFailed, # of errors = %d%s\n", Red, n_err, Reset)
	} else {
		fmt.Fprintf(os.Stderr, "%sPASS%s\n", Green, Reset)
		DbPrintf("debug", "\n\n%sPASS%s\n", Green, Reset)
	}
}
Пример #4
0
func (s *LambdaClosureTestSuite) TestLexie(c *C) {

	return
	fmt.Fprintf(os.Stderr, "Test NFA generation from REs, %s\n", tr.LF())

	n_err := 0

	dbOn["db_NFA"] = true
	dbOn["db_NFA_LnNo"] = true
	dbOn["db_DumpPool"] = true
	dbOn["parseExpression"] = true

	// {Test: "0011", Re: `a(bcd)*(ghi)+(jkl)*X`, Rv: 1011},     //
	Pool := nfa.NewNFA_Pool()
	Cur := Pool.GetNFA()
	Pool.InitState = Cur

	vv := Lexie01Data[11]
	Pool.AddReInfo(vv.Re, "", 1, vv.Rv, nfa.InfoType{})
	Pool.Sigma = Pool.GenerateSigma()
	fmt.Printf("\n\nRe: %s\n", vv.Re)
	Pool.DumpPool(false)

	// -------------------------------------- test 1 -----------------------------------
	r1 := Pool.LambdaClosure([]int{1})
	fmt.Printf("\n\nr1(1)=%v\n", r1)

	if len(com.CompareSlices([]int{4, 1, 5, 6}, r1)) != 0 {
		fmt.Printf("%sError%s: Test case 1 failed to match\n", Red, Reset)
		n_err++
	}
	// c.Check(r1, Equals, []int{4, 1, 5, 6})
	c.Check(len(com.CompareSlices([]int{4, 1, 5, 6}, r1)), Equals, 0)

	// -------------------------------------- test 2 -----------------------------------
	r2 := Pool.LambdaClosure([]int{5, 8, 11})
	fmt.Printf("\n\nr2(5,8,11)=%v\n", r2)

	if len(com.CompareSlices([]int{6}, r2)) != 0 {
		fmt.Printf("%sError%s: Test case 2 failed to match\n", Red, Reset)
		n_err++
	}
	// c.Check(r2, Equals, []int{5, 8, 11})
	c.Check(len(com.CompareSlices([]int{6}, r2)), Equals, 0)

	// -------------------------------------- test 3 -----------------------------------
	r3 := Pool.LambdaClosure([]int{9})
	fmt.Printf("\n\nr3(9)=%v\n", r3)

	if len(com.CompareSlices([]int{6, 10, 13, 10, 14}, r3)) != 0 {
		fmt.Printf("%sError%s: Test case 3 failed to match\n", Red, Reset)
		n_err++
	}
	// c.Check(r3, Equals, []int{5, 8, 11})
	c.Check(len(com.CompareSlices([]int{6, 10, 13, 10, 14}, r3)), Equals, 0)

	// ------------------------- understand test runner ---------------------------------------
	if false {
		c.Check(1, Equals, 0)
		// c.Assert(2, Equals, 0) // Failure of an assert ends test (exit)
		sss := c.GetTestLog()
		fp, err := Fopen(",g", "w")
		c.Check(err, Equals, nil)
		fmt.Fprintf(fp, "c.GetTestLog: ->%s<-\n", sss)
	}

	// ------------------------- eval results now ---------------------------------------

	if n_err > 0 {
		fmt.Fprintf(os.Stderr, "%sFailed, # of errors = %d%s\n", Red, n_err, Reset)
		DbPrintf("debug", "\n\n%sFailed, # of errors = %d%s\n", Red, n_err, Reset)
	} else {
		fmt.Fprintf(os.Stderr, "%sPASS%s\n", Green, Reset)
		DbPrintf("debug", "\n\n%sPASS%s\n", Green, Reset)
	}
}