aboutsummaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
Diffstat (limited to 'cmd')
-rw-r--r--cmd/ucdgen/main.go98
-rw-r--r--cmd/vartan-go/generate.go10
-rw-r--r--cmd/vartan/compile.go20
-rw-r--r--cmd/vartan/parse.go4
-rw-r--r--cmd/vartan/test.go9
5 files changed, 115 insertions, 26 deletions
diff --git a/cmd/ucdgen/main.go b/cmd/ucdgen/main.go
new file mode 100644
index 0000000..6d7d33e
--- /dev/null
+++ b/cmd/ucdgen/main.go
@@ -0,0 +1,98 @@
+package main
+
+import (
+ "fmt"
+ "net/http"
+ "os"
+ "strings"
+ "text/template"
+
+ "github.com/nihei9/vartan/ucd"
+)
+
+func main() {
+ err := gen()
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "%v\n", err)
+ os.Exit(1)
+ }
+}
+
+func gen() error {
+ var propValAliases *ucd.PropertyValueAliases
+ {
+ resp, err := http.Get("https://www.unicode.org/Public/13.0.0/ucd/PropertyValueAliases.txt")
+ if err != nil {
+ return err
+ }
+ defer resp.Body.Close()
+ propValAliases, err = ucd.ParsePropertyValueAliases(resp.Body)
+ if err != nil {
+ return err
+ }
+ }
+ var unicodeData *ucd.UnicodeData
+ {
+ resp, err := http.Get("https://www.unicode.org/Public/13.0.0/ucd/UnicodeData.txt")
+ if err != nil {
+ return err
+ }
+ defer resp.Body.Close()
+ unicodeData, err = ucd.ParseUnicodeData(resp.Body, propValAliases)
+ if err != nil {
+ return err
+ }
+ }
+ var scripts *ucd.Scripts
+ {
+ resp, err := http.Get("https://www.unicode.org/Public/13.0.0/ucd/Scripts.txt")
+ if err != nil {
+ return err
+ }
+ defer resp.Body.Close()
+ scripts, err = ucd.ParseScripts(resp.Body, propValAliases)
+ if err != nil {
+ return err
+ }
+ }
+ var propList *ucd.PropList
+ {
+ resp, err := http.Get("https://www.unicode.org/Public/13.0.0/ucd/PropList.txt")
+ if err != nil {
+ return err
+ }
+ defer resp.Body.Close()
+ propList, err = ucd.ParsePropList(resp.Body)
+ if err != nil {
+ return err
+ }
+ }
+ tmpl, err := template.ParseFiles("../ucd/codepoint.go.tmpl")
+ if err != nil {
+ return err
+ }
+ var b strings.Builder
+ err = tmpl.Execute(&b, struct {
+ GeneratorName string
+ UnicodeData *ucd.UnicodeData
+ Scripts *ucd.Scripts
+ PropList *ucd.PropList
+ PropertyValueAliases *ucd.PropertyValueAliases
+ }{
+ GeneratorName: "generator/main.go",
+ UnicodeData: unicodeData,
+ Scripts: scripts,
+ PropList: propList,
+ PropertyValueAliases: propValAliases,
+ })
+ if err != nil {
+ return err
+ }
+ f, err := os.OpenFile("../ucd/codepoint.go", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
+ if err != nil {
+ return err
+ }
+ defer f.Close()
+ fmt.Fprint(f, b.String())
+ return nil
+}
diff --git a/cmd/vartan-go/generate.go b/cmd/vartan-go/generate.go
index 13ba5c0..7d95ed7 100644
--- a/cmd/vartan-go/generate.go
+++ b/cmd/vartan-go/generate.go
@@ -6,8 +6,8 @@ import (
"io"
"os"
- mldriver "github.com/nihei9/maleeni/driver"
- "github.com/nihei9/vartan/driver"
+ "github.com/nihei9/vartan/driver/lexer"
+ "github.com/nihei9/vartan/driver/parser"
spec "github.com/nihei9/vartan/spec/grammar"
"github.com/spf13/cobra"
)
@@ -42,7 +42,7 @@ func runGenerate(cmd *cobra.Command, args []string) error {
}
{
- b, err := mldriver.GenLexer(cgram.LexicalSpecification.Maleeni.Spec, *generateFlags.pkgName)
+ b, err := lexer.GenLexer(cgram.Lexical, *generateFlags.pkgName)
if err != nil {
return fmt.Errorf("Failed to generate a lexer: %w", err)
}
@@ -62,7 +62,7 @@ func runGenerate(cmd *cobra.Command, args []string) error {
}
{
- b, err := driver.GenParser(cgram, *generateFlags.pkgName)
+ b, err := parser.GenParser(cgram, *generateFlags.pkgName)
if err != nil {
return fmt.Errorf("Failed to generate a parser: %w", err)
}
@@ -82,7 +82,7 @@ func runGenerate(cmd *cobra.Command, args []string) error {
}
{
- b, err := driver.GenSemanticAction(*generateFlags.pkgName)
+ b, err := parser.GenSemanticAction(*generateFlags.pkgName)
if err != nil {
return fmt.Errorf("Failed to generate a semantic action set: %w", err)
}
diff --git a/cmd/vartan/compile.go b/cmd/vartan/compile.go
index 49e383d..e645366 100644
--- a/cmd/vartan/compile.go
+++ b/cmd/vartan/compile.go
@@ -10,6 +10,7 @@ import (
verr "github.com/nihei9/vartan/error"
"github.com/nihei9/vartan/grammar"
spec "github.com/nihei9/vartan/spec/grammar"
+ "github.com/nihei9/vartan/spec/grammar/parser"
"github.com/spf13/cobra"
)
@@ -78,17 +79,12 @@ func runCompile(cmd *cobra.Command, args []string) (retErr error) {
}
}
- gram, err := readGrammar(grmPath)
+ gram, report, err := readGrammar(grmPath)
if err != nil {
return err
}
- cgram, report, err := grammar.Compile(gram, grammar.EnableReporting())
- if err != nil {
- return err
- }
-
- err = writeCompiledGrammarAndReport(cgram, report, *compileFlags.output)
+ err = writeCompiledGrammarAndReport(gram, report, *compileFlags.output)
if err != nil {
return fmt.Errorf("Cannot write an output files: %w", err)
}
@@ -113,22 +109,22 @@ func runCompile(cmd *cobra.Command, args []string) (retErr error) {
return nil
}
-func readGrammar(path string) (grm *grammar.Grammar, retErr error) {
+func readGrammar(path string) (*spec.CompiledGrammar, *spec.Report, error) {
f, err := os.Open(path)
if err != nil {
- return nil, fmt.Errorf("Cannot open the grammar file %s: %w", path, err)
+ return nil, nil, fmt.Errorf("Cannot open the grammar file %s: %w", path, err)
}
defer f.Close()
- ast, err := spec.Parse(f)
+ ast, err := parser.Parse(f)
if err != nil {
- return nil, err
+ return nil, nil, err
}
b := grammar.GrammarBuilder{
AST: ast,
}
- return b.Build()
+ return b.Build(grammar.EnableReporting())
}
// writeCompiledGrammarAndReport writes a compiled grammar and a report to a files located at a specified path.
diff --git a/cmd/vartan/parse.go b/cmd/vartan/parse.go
index ed35a60..f21a03b 100644
--- a/cmd/vartan/parse.go
+++ b/cmd/vartan/parse.go
@@ -7,7 +7,7 @@ import (
"os"
"strings"
- "github.com/nihei9/vartan/driver"
+ driver "github.com/nihei9/vartan/driver/parser"
spec "github.com/nihei9/vartan/spec/grammar"
"github.com/nihei9/vartan/tester"
"github.com/spf13/cobra"
@@ -172,7 +172,7 @@ func writeSyntaxErrorMessage(b *strings.Builder, cgram *spec.CompiledGrammar, sy
case tok.Invalid():
fmt.Fprintf(b, "'%v' (<invalid>)", string(tok.Lexeme()))
default:
- if kind := cgram.ParsingTable.Terminals[tok.TerminalID()]; kind != "" {
+ if kind := cgram.Syntactic.Terminals[tok.TerminalID()]; kind != "" {
fmt.Fprintf(b, "'%v' (%v)", string(tok.Lexeme()), kind)
} else {
fmt.Fprintf(b, "'%v'", string(tok.Lexeme()))
diff --git a/cmd/vartan/test.go b/cmd/vartan/test.go
index 50ba8ca..9bd88ff 100644
--- a/cmd/vartan/test.go
+++ b/cmd/vartan/test.go
@@ -5,7 +5,6 @@ import (
"fmt"
"os"
- "github.com/nihei9/vartan/grammar"
"github.com/nihei9/vartan/tester"
"github.com/spf13/cobra"
)
@@ -22,14 +21,10 @@ func init() {
}
func runTest(cmd *cobra.Command, args []string) error {
- g, err := readGrammar(args[0])
+ gram, _, err := readGrammar(args[0])
if err != nil {
return fmt.Errorf("Cannot read a grammar: %w", err)
}
- cg, _, err := grammar.Compile(g)
- if err != nil {
- return fmt.Errorf("Cannot read a compiled grammar: %w", err)
- }
var cs []*tester.TestCaseWithMetadata
{
@@ -47,7 +42,7 @@ func runTest(cmd *cobra.Command, args []string) error {
}
t := &tester.Tester{
- Grammar: cg,
+ Grammar: gram,
Cases: cs,
}
rs := t.Run()