modernc.org/knuth@v0.0.4/mft/generate.go (about)

     1  // Copyright 2023 The Knuth Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build ignore
     6  // +build ignore
     7  
     8  package main
     9  
    10  import (
    11  	"fmt"
    12  	"os"
    13  	"path/filepath"
    14  
    15  	"modernc.org/knuth"
    16  	"modernc.org/knuth/web"
    17  )
    18  
    19  func fail(s string, args ...interface{}) {
    20  	fmt.Fprintf(os.Stderr, s, args...)
    21  	os.Exit(1)
    22  }
    23  
    24  func main() {
    25  	const (
    26  		base = "mft"
    27  
    28  		chFn   = base + ".ch"
    29  		goFn   = base + ".go"
    30  		pasFn  = base + ".pas"
    31  		poolFn = base + ".pool"
    32  		webFn  = base + ".web"
    33  	)
    34  
    35  	dest, err := os.Create(filepath.FromSlash(goFn))
    36  	if err != nil {
    37  		fail("creating %s: %v\n", goFn, err)
    38  	}
    39  
    40  	defer func() {
    41  		if err = dest.Close(); err != nil {
    42  			fail("closing %s: %v\n", goFn, err)
    43  		}
    44  	}()
    45  
    46  	pas, err := os.Create(filepath.FromSlash(pasFn))
    47  	if err != nil {
    48  		fail("creating %s: %v\n", pasFn, err)
    49  	}
    50  
    51  	defer func() {
    52  		if err = pas.Close(); err != nil {
    53  			fail("closing %s: %v\n", pasFn, err)
    54  		}
    55  	}()
    56  
    57  	pool, err := os.Create(filepath.FromSlash(poolFn))
    58  	if err != nil {
    59  		fail("creating %s: %v\n", poolFn, err)
    60  	}
    61  
    62  	defer func() {
    63  		if err = pool.Close(); err != nil {
    64  			fail("closing %s: %v\n", poolFn, err)
    65  		}
    66  	}()
    67  
    68  	webSrc, err := os.ReadFile(filepath.FromSlash(webFn))
    69  	if err != nil {
    70  		fail("reading %v: %v\n", webFn, err)
    71  	}
    72  
    73  	chSrc, err := os.ReadFile(filepath.FromSlash(chFn))
    74  	if err != nil {
    75  		fail("reading %v: %v\n", chFn, err)
    76  	}
    77  
    78  	src, err := knuth.NewChanger(
    79  		knuth.NewRuneSource(webFn, webSrc, knuth.Unicode),
    80  		knuth.NewRuneSource(chFn, chSrc, knuth.Unicode),
    81  	)
    82  	if err != nil {
    83  		fail("processing %s and %s: %v\n", webFn, chFn, err)
    84  	}
    85  
    86  	if err := web.Go(dest, pas, pool, src, base); err != nil {
    87  		fail("generate: %v\n", err)
    88  	}
    89  }