Пример #1
0
func testParse(src string, r Rule) (n int, tok token.Token) {
	eat := 0
	ok := false
	parser.Fast(
		[]byte(src),
		func(_ scanner.Pos, t token.Token, _ string) error {

			if t == token.EOF {
				if !ok {
					// 让 More 结束
					_, ok = r.Eat(t)
				}
				if ok {
					tok = token.EOF
				}
				return nil
			}

			// 去占位,
			if t > token.IDENT {
				return nil
			}
			if t == token.NL {
				// 合并连续的换行, 清理排版换行
				switch tok {
				case token.NL, token.EOF, token.COMMA, token.SEMICOLON,
					token.LEFT, token.RIGHT:
					return nil
				}
				// 推延换行
				tok = token.NL
				return nil
			}

			if tok == token.NL {
				switch t {
				case token.COMMA, token.SEMICOLON, token.RIGHT:
				default:
					// 换行改 ";"
					eat, ok = r.Eat(token.SEMICOLON)
					if eat == 0 {
						return io.ErrNoProgress
					}
					n++
				}
			}

			tok = t
			eat, ok = r.Eat(t)

			if eat == 0 {
				return io.ErrNoProgress
			}
			n++
			return nil
		})

	return
}
Пример #2
0
func Test_eq(t *testing.T) {
	for _, ss := range good {
		nodes, err := parser.Fast([]byte(ss[0]), nil)
		if err != nil {
			t.Fatal(err)
		}
		if len(nodes) != len(ss)-1 {
			t.Fatal(nodes)
		}
		ss = ss[1:]

		for i, n := range nodes {
			if ss[i] != n.Source {
				t.Fatal(n)
			}
		}
	}
}