Skip to content

Commit a047e90

Browse files
committed
Some basic refactoring
1 parent 08aec33 commit a047e90

File tree

1 file changed

+33
-21
lines changed
  • csv-validator-core/src/main/scala/uk/gov/nationalarchives/csv/validator/schema

1 file changed

+33
-21
lines changed

csv-validator-core/src/main/scala/uk/gov/nationalarchives/csv/validator/schema/SchemaParser.scala

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,11 @@ trait SchemaParser extends RegexParsers {
129129
//def combinatorialAndNonConditionalRule = (and | or | nonConditionalRule)
130130
//def nonConditionalRule = opt( columnRef <~ "/") ~ unaryRule ^^ { case explicitColumn ~ rule => rule.explicitColumn = explicitColumn; rule }
131131

132+
//TODO Update EBNF
132133
def columnValidationExpr: Parser[Rule] = positioned(combinatorialExpr | nonCombinatorialExpr)
133-
//def columnValidationExpr: Parser[Rule] = combinatorialExpr | nonCombinatorialExpr //TODO reinstate positioned above
134+
135+
//TODO reorder EBNF
136+
def combinatorialExpr = orExpr | andExpr
134137

135138
def nonCombinatorialExpr = nonConditionalExpr | conditionalExpr
136139

@@ -182,11 +185,15 @@ trait SchemaParser extends RegexParsers {
182185
//val Regex = """([(]")(.*?)("[)])""".r
183186
//val regexParser: Parser[String] = Regex withFailureMessage("""regex not correctly delimited as ("your regex")""")
184187

185-
def rangeExpr: Parser[RangeRule] = "range(" ~> numericLiteral ~ "," ~ numericLiteral <~ ")" ^^ { case a ~ "," ~ b => RangeRule(a, b) }
188+
def rangeExpr: Parser[RangeRule] = "range(" ~> numericLiteral ~ "," ~ numericLiteral <~ ")" ^^ {
189+
case a ~ "," ~ b =>
190+
RangeRule(a, b)
191+
}
186192

187193
//TODO refactor
188194
def lengthExpr: Parser[LengthRule] = ("length(" ~> opt(positiveIntegerOrAny <~ ",") ~ positiveIntegerOrAny <~ ")") ^^ {
189-
case from ~ to => LengthRule(from, to)
195+
case from ~ to =>
196+
LengthRule(from, to)
190197
}
191198

192199
def positiveIntegerOrAny = (positiveIntegerLiteral | wildcardLiteral) ^^ {
@@ -197,10 +204,7 @@ trait SchemaParser extends RegexParsers {
197204

198205
def notEmptyExpr = "notEmpty" ^^^ NotEmptyRule()
199206

200-
//TODO rewrite uniqueExpr and uniqueMultiExpr into single expression, will need to refactor UniqueRule and UniqueMultiRule
201-
//def uniqueExpr: Parser[UniqueRule] = "unique" ^^^ UniqueRule()
202-
//def uniqueMultiExpr: Parser[UniqueMultiRule] = "unique(" ~> columnRef ~ rep(',' ~> columnRef) <~ ")" ^^ { s => UniqueMultiRule( s._1 :: s._2 ) }
203-
def uniqueExpr: Parser[Rule] = "unique" ~> opt("(" ~> columnRef ~ rep(',' ~> columnRef) <~ ")") ^^ {
207+
def uniqueExpr: Parser[Rule] = "unique" ~> opt("(" ~> columnRef ~ rep("," ~> columnRef) <~ ")") ^^ {
204208
case None =>
205209
UniqueRule()
206210
case Some((columnRef1 ~ columnRefN)) =>
@@ -220,7 +224,8 @@ trait SchemaParser extends RegexParsers {
220224
def xDateTime: Parser[XsdDateTimeRule] = "xDateTime" ^^^ XsdDateTimeRule()
221225

222226
def xDateTimeRange: Parser[XsdDateTimeRangeRule] = "xDateTime(" ~> xDateTimeExpr ~ "," ~ xDateTimeExpr <~ ")" ^^ {
223-
case from ~ "," ~ to => XsdDateTimeRangeRule(from, to)
227+
case from ~ "," ~ to =>
228+
XsdDateTimeRangeRule(from, to)
224229
}
225230

226231
//TODO change to literal
@@ -233,7 +238,8 @@ trait SchemaParser extends RegexParsers {
233238
def xDate: Parser[XsdDateRule] = "xDate" ^^^ XsdDateRule()
234239

235240
def xDateRange: Parser[XsdDateRangeRule] = "xDate(" ~> xDateExpr ~ "," ~ xDateExpr <~ ")" ^^ {
236-
case from ~ "," ~ to => XsdDateRangeRule(from, to)
241+
case from ~ "," ~ to =>
242+
XsdDateRangeRule(from, to)
237243
}
238244

239245
//TODO change to literal
@@ -246,7 +252,8 @@ trait SchemaParser extends RegexParsers {
246252
def xTime: Parser[XsdTimeRule] = "xTime" ^^^ XsdTimeRule()
247253

248254
def xTimeRange: Parser[XsdTimeRangeRule] = "xTime(" ~> xTimeExpr ~ "," ~ xTimeExpr <~ ")" ^^ {
249-
case from ~ "," ~ to => XsdTimeRangeRule(from, to)
255+
case from ~ "," ~ to =>
256+
XsdTimeRangeRule(from, to)
250257
}
251258

252259
//TODO change to literal
@@ -259,7 +266,8 @@ trait SchemaParser extends RegexParsers {
259266
def ukDate: Parser[UkDateRule] = "ukDate" ^^^ UkDateRule()
260267

261268
def ukDateRange: Parser[UkDateRangeRule] = "ukDate(" ~> ukDateStringLiteral ~ "," ~ ukDateStringLiteral <~ ")" ^^ {
262-
case from ~ "," ~ to => UkDateRangeRule(from, to)
269+
case from ~ "," ~ to =>
270+
UkDateRangeRule(from, to)
263271
}
264272

265273
//TODO change to literal
@@ -284,6 +292,20 @@ trait SchemaParser extends RegexParsers {
284292
rule
285293
}
286294

295+
def fileExistsExpr = ("fileExists" ~> opt("(" ~> stringProvider <~ ")")).withFailureMessage("Invalid fileExists rule") ^^ {
296+
case None =>
297+
FileExistsRule(pathSubstitutions, enforceCaseSensitivePathChecks)
298+
case Some(s) =>
299+
FileExistsRule(pathSubstitutions, enforceCaseSensitivePathChecks, s)
300+
}
301+
302+
def checksumExpr = "checksum(" ~> file ~ "," ~ algorithmExpr <~ ")" ^^ {
303+
case files ~ "," ~ algorithm =>
304+
ChecksumRule(files._1.getOrElse(Literal(None)), files._2, algorithm, pathSubstitutions, enforceCaseSensitivePathChecks)
305+
}
306+
307+
def fileCountExpr = "fileCount(" ~> file <~ ")" ^^ { case a => FileCountRule(a._1.getOrElse(Literal(None)), a._2, pathSubstitutions) }
308+
287309
//TODO below **** MUCH TODO!
288310

289311
//TODO update EBNF to use simpler lexical rules
@@ -311,8 +333,6 @@ trait SchemaParser extends RegexParsers {
311333
val nonBreakingCharPattern = """[^\r\n\f]"""
312334
//</editor-fold>
313335

314-
def combinatorialExpr = orExpr | andExpr
315-
316336
// def unaryRule =
317337
// parenthesesRule | in | is | isNot | starts | ends |
318338
// empty | notEmpty |
@@ -349,16 +369,8 @@ trait SchemaParser extends RegexParsers {
349369

350370
//def fileArgProvider: Parser[ArgProvider] = columnRef ^^ { s => ColumnReference(s) } | '\"' ~> rootFilePath <~ '\"' ^^ {s => Literal(Some(s)) }
351371

352-
//TODO refactor into single expression
353-
def fileExistsExpr = ("fileExists(" ~> stringProvider <~ ")" ^^ { s => FileExistsRule(pathSubstitutions, enforceCaseSensitivePathChecks, s) }).withFailureMessage("fileExists rule has an invalid file path") |
354-
"fileExists" ^^^ { FileExistsRule( pathSubstitutions, enforceCaseSensitivePathChecks ) } | failure("Invalid fileExists rule")
355-
356372
def rootFilePath: Parser[String] = """[\^&'@\{\}\[\]\,\$=!\-#\(\)%\.\+~_a-zA-Z0-9\s\\/:]+""".r //Characters taken from http://support.microsoft.com/kb/177506 and added '/' and '\' and ':'
357373

358-
def checksumExpr = "checksum(" ~> file ~ "," ~ algorithmExpr <~ ")" ^^ { case files ~ "," ~ algorithm => ChecksumRule(files._1.getOrElse(Literal(None)), files._2, algorithm, pathSubstitutions, enforceCaseSensitivePathChecks) }
359-
360-
def fileCountExpr = "fileCount(" ~> file <~ ")" ^^ { case a => FileCountRule(a._1.getOrElse(Literal(None)), a._2, pathSubstitutions) }
361-
362374
def dateRange = "dateRange(\""
363375

364376
def file = "file(" ~> opt(stringProvider <~ ",") ~ stringProvider <~ ")"

0 commit comments

Comments
 (0)