Add the "verify" script

In addition to the new script, this changes the V2 parser to be able
to continue past the first error found so that as many errors as
possible may be displayed.
This commit is contained in:
Andrew Miner
2015-01-14 15:57:27 -08:00
parent 89a8e2b539
commit 291f94da57
3 changed files with 64 additions and 11 deletions
Executable
+34
View File
@@ -0,0 +1,34 @@
#!/usr/bin/env coffee
global._ = require 'underscore'
global.Backbone = require 'backbone'
fs = require 'fs'
Logger = require '../src/scripts/logger'
ModVersionParser = require '../src/scripts/models/mod_version_parser'
require '../src/scripts/underscore_mixins'
########################################################################################################################
if process.argv.length is 3
sourceFileName = process.argv[2]
else
console.error """
USAGE: verify <source>
Verifies that a recipe book is in the proper format. If there is a problem,
then a message will be displayed. Otherwise, the command has no output.
"""
process.exit -1
global.logger = new Logger level:Logger.WARNING
try
text = fs.readFileSync sourceFileName, 'UTF-8'
parser = new ModVersionParser showAllErrors:true
modVersion = parser.parse text
catch e
console.error e.message
+5 -3
View File
@@ -15,10 +15,12 @@ module.exports = class ModVersionParser
@CURRENT_VERSION = '2' @CURRENT_VERSION = '2'
constructor: -> constructor: (options={})->
options.showAllErrors ?= false
@_parsers = @_parsers =
'1': new V1 '1': new V1 options
'2': new V2 '2': new V2 options
parse: (data)-> parse: (data)->
if not data? then throw new Error 'mod description data is missing' if not data? then throw new Error 'mod description data is missing'
@@ -15,6 +15,12 @@ StringBuilder = require '../string_builder'
module.exports = class V2 module.exports = class V2
constructor: (options={})->
options.showAllErrors ?= false
@showAllErrors = options.showAllErrors
# Class Methods ################################################################################
@COMMAND = /\ *([^:]*):?(.*)/ @COMMAND = /\ *([^:]*):?(.*)/
@COMMENT = /([^\\]?)#.*/ @COMMENT = /([^\\]?)#.*/
@@ -25,6 +31,8 @@ module.exports = class V2
@STACK = /^([0-9]+) +(.*)$/ @STACK = /^([0-9]+) +(.*)$/
# Public Methods ###############################################################################
parse: (text)-> parse: (text)->
@_modVersionData = {} @_modVersionData = {}
@_lineNumber = 1 @_lineNumber = 1
@@ -36,7 +44,8 @@ module.exports = class V2
for command in commands for command in commands
@_execute command @_execute command
return @_buildModVersion @_modVersionData modVersion = @_handleErrors @_buildModVersion, @_modVersionData
return modVersion
unparse: (modVersion)-> unparse: (modVersion)->
builder = new StringBuilder context:modVersion builder = new StringBuilder context:modVersion
@@ -48,11 +57,8 @@ module.exports = class V2
_execute: (command)-> _execute: (command)->
method = this["_command_#{command.name}"] method = this["_command_#{command.name}"]
if not method? then throw new Error "Unknown command: #{command.name}" if not method? then throw new Error "Unknown command: #{command.name}"
try
method.apply this, command.args @_handleErrors method, command.args
catch e
e.message = "line #{@_lineNumber}: #{e.message}"
throw e
_parseLine: (line)-> _parseLine: (line)->
line = line.replace V2.COMMENT, '$1' line = line.replace V2.COMMENT, '$1'
@@ -73,6 +79,17 @@ module.exports = class V2
return commands return commands
_handleErrors: (callback, args...)->
if args.length is 1 and _.isArray(args[0]) then args = args[0]
try
callback.apply this, args
catch e
e.message = "line #{@_lineNumber}: #{e.message}"
if not @showAllErrors then throw e
console.error e.message
# Command Methods ############################################################################## # Command Methods ##############################################################################
_command_description: (descriptionParts...)-> _command_description: (descriptionParts...)->
@@ -178,7 +195,7 @@ module.exports = class V2
modVersion = new ModVersion attributes modVersion = new ModVersion attributes
for itemData in modVersionData.items for itemData in modVersionData.items
@_buildItem modVersion, itemData @_handleErrors @_buildItem, modVersion, itemData
return modVersion return modVersion
@@ -190,7 +207,7 @@ module.exports = class V2
item = new Item modVersion:modVersion, name:itemData.name, isGatherable:itemData.gatherable item = new Item modVersion:modVersion, name:itemData.name, isGatherable:itemData.gatherable
for recipeData in itemData.recipes for recipeData in itemData.recipes
@_buildRecipe modVersion, item, recipeData @_handleErrors @_buildRecipe, modVersion, item, recipeData
return item return item