Commit 845d76cf authored by Laurent Le Brun's avatar Laurent Le Brun
Browse files

Fix parse error when keyword is followed by underscore

For example "do_something" was a parse error, due a confusion with the
"do" keyword.
Also, some more code cleanup.
parent cc56a36d
module Ast
open System.Collections.Generic
open System.IO
type targetOutput = Text | CHeader | CList | JS | Nasm
let version = "1.1.5" // Shader Minifer version
let debugMode = false
let debugMode = true
let nullOut = new StreamWriter(Stream.Null) :> TextWriter
let mutable outputName = "shader_code.h"
let mutable targetOutput = CHeader
......@@ -93,10 +96,10 @@ let mapEnv fe fi = {fExpr = fe; fInstr = fi; vars = Map.empty}
let foldList env fct li =
let env = ref env
let res = li |> List.map ((fun i -> // FIXME: use List.fold is cleaner :)
let res = li |> List.map (fun i -> // FIXME: use List.fold is cleaner :)
let x = fct !env i
env := fst x
snd x) : 'a -> 'a)
snd x)
!env, res
// Applies env.fExpr recursively on all nodes of an expression.
......
......@@ -55,7 +55,7 @@ let printHeader data asAList =
let printNoHeader data =
use out = output()
let str = [for _,code in data -> Printer.print code] |> String.concat "\n"
let str = [for _, code in data -> Printer.print code] |> String.concat "\n"
fprintf out "%s" str
let printJSHeader data =
......@@ -95,8 +95,7 @@ let printNasmHeader data =
fprintfn out "_%s:\r\n\tdb '%s', 0" name (Printer.print code)
fprintfn out ""
let print data =
match Ast.targetOutput with
let print data = function
| Ast.Text -> printNoHeader data
| Ast.CHeader -> printHeader data false
| Ast.CList -> printHeader data true
......
......@@ -26,7 +26,7 @@ let nullOut = new StreamWriter(Stream.Null) :> TextWriter
// like printf when verbose option is set
let vprintf fmt =
let out = if Ast.verbose then stdout else nullOut
let out = if Ast.verbose then stdout else Ast.nullOut
fprintf out fmt
let printSize code =
......@@ -52,9 +52,7 @@ let readFile file =
else new StreamReader(file)
stream.ReadToEnd()
let minify file =
let content = readFile file
let filename = if file = "" then "stdin" else file
let minify filename (content: string) =
vprintf "Input file size is: %d\n" (content.Length)
let code = Parse.runParser filename content
vprintf "File parsed. "; printSize code
......@@ -68,17 +66,22 @@ let minify file =
if Ast.noRenaming then code
else rename code
vprintf "Minification of '%s' finished.\n" file
vprintf "Minification of '%s' finished.\n" filename
code
let minifyFile file =
let content = readFile file
let filename = if file = "" then "stdin" else file
minify filename content
let run files =
let fail (exn:exn) s =
printfn "%s" s;
printfn "%s" exn.StackTrace
1
try
let codes = Array.map minify files
CGen.print (Array.zip files codes)
let codes = Array.map minifyFile files
CGen.print (Array.zip files codes) Ast.targetOutput
0
with
| Failure s as exn -> fail exn s
......@@ -124,7 +127,7 @@ let () =
//"--macro-threshold", ArgType.Int (fun i ->
// printfn "Macros are disabled in the release."; Ast.macroThreshold <- i), "[disabled] Use a #define macro if it can save at least <int> bytes"
"--", ArgType.Rest setFile, "Stop parsing command line"
] |> List.map (fun (s, f, d) -> ArgInfo(s, f, d))
] |> List.map ArgInfo
ArgParser.Parse(specs, setFile)
files := List.rev !files
......
......@@ -143,7 +143,7 @@ module private ParseImpl =
let statement, stmtRef = createParserForwardedToRef()
let declaration, declRef = createParserForwardedToRef()
let keyword s = attempt (pstring s .>> notFollowedBy letter .>> notFollowedBy digit) .>> ws
let keyword s = attempt (pstring s .>> notFollowedBy letter .>> notFollowedBy digit .>> notFollowedBy (ch '_')) .>> ws
// A type block, like struct or interface blocks
let blockSpecifier prefix =
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment