I am reviewing some old code in light of some new understanding of parsing. I have a brute-force function that checks a string to see if it is a valid COBOL word, that is, starts with a letter, contains only letters, digits, or the hyphen, and is no more than 30 characters long.
I am trying to perform that check using parse, and I think I have got it, except for the length restriction of 30 characters. I am wondering if a parse rule can check the parsed data for its length, or if that check is best done elsewhere. In other words, check for the maximum length, and THEN parse it for valid characters.
Thank you. Code sample follows.
R E B O L [Title: "Test for COBOL word"]
LETTER: charset [#"A" - #"Z"]
DIGIT: charset [#"0" - #"9"]
VALIDCHARACTER: [some LETTER | some DIGIT | #"-"]
COBOLWORD: [1 LETTER some VALIDCHARACTER]
print parse "123456" COBOLWORD ;; should be false; starts with number
print parse "ABCDEF" COBOLWORD ;; should be true; all letters
print parse "A-1-STEAK-SAUCE" COBOLWORD ;; should be true; starts with letter
print parse "4runner" COBOLWORD ;; should be false; starts with number
print parse "$ average" COBOLWORD ;; should be false; invalid character
print parse "A----BCDE" COBOLWORD ;; should be true; multiple - allowed
halt