Skip to main content

module lang::cobol85::\syntax::Lexical

rascal-0.43.3
org.rascalmpl.cobol-air-0.1.2

The lexical tokens of COBOL: words, numbers, nonnumeric literals, PICTURE strings and REPLACE directives.

Usage​

import lang::cobol85::\syntax::Lexical;

Dependencies​

extend lang::cobol85::\syntax::Layout;

Description​

This module defines the tokens that the rest of the grammar is built from. Id is the single lexical for user-defined COBOL words, excluding the reserved words that the modules declare in CobwordKeywords. Num is the only number lexical; it distinguishes decimals from integers, and a sign glued to its digits makes a signed literal, so -1 is a number but A - 1 is a subtraction. Nonnumeric literals come in double and single quotes, with doubled quotes inside, and may be continued over several lines up to the margin. PictureString reads the character-string of a PICTURE clause by longest match, so a trailing comma or period ends the clause rather than the picture. The module also defines pseudo-text (==...==) and the REPLACE directive. Follow restrictions, instead of spacing rules, keep the tokens apart; for example Digits may not be followed by a comma and a digit, so 1,5 is one decimal number.

syntax Nonnumeric​

syntax Nonnumeric = NonnumericPart* NonnumericWhole;

syntax NonnumericPart​

lexical NonnumericPart
= @category="string.quoted.double" [\"] DoubleStrChar* !>> ![\n\uE000]
| @category="string.quoted.single" [\'] SingleStrChar* !>> ![\n\uE000]
;

syntax NonnumericWhole​

lexical NonnumericWhole
= @category="string.quoted.double" [\"] DoubleStrChar* [\"] !>> [\"]
| @category="string.quoted.single" [\'] SingleStrChar* [\'] !>> [\']
;

syntax DoubleStrChar​

lexical DoubleStrChar
= ![\"\n\uE000]
| [\"] [\"]
;

syntax SingleStrChar​

lexical SingleStrChar
= ![\'\n\uE000]
| [\'] [\']
// assuming same convention as for "
;

syntax PictureString​

lexical PictureString = @category="type" [\t-\n\ ,.;\uE000] << PictureContents \ CobwordKeywords !>> ![\t-\n\ ,.;\uE000] !>> ",." !>> ";.";

syntax PictureContents​

lexical PictureContents = PictureChar+;

syntax PictureChar​

lexical PictureChar
= [,.;] >> ![\t-\n\ \uE000]
| ![\t-\n\ ,.;\uE000]
;

syntax ReplaceDirective​

syntax ReplaceDirective
= replaceOff: 'REPLACE' 'OFF' "."
| replace: 'REPLACE' ReplacePseudoTextPair+ "."
;

syntax ReplacePseudoTextPair​

syntax ReplacePseudoTextPair = by: QuotedPseudoText 'BY' QuotedPseudoText;

syntax QuotedPseudoText​

lexical QuotedPseudoText = "==" (![=] | ("=" ![=]))* "==";

syntax Num​

lexical Num
= decimal: [0-9\-+] !<< [0-9]* [,.] [0-9]+ !>> [0-9]
| signedDecimal: [0-9A-Za-z.] !<< [+\-] [0-9]* [,.] [0-9]+ !>> [0-9]
| signedInteger: [0-9A-Za-z.] !<< [+\-] Digits
| integer: [0-9\-+.A-Za-z] !<< Digits
;

syntax Digits​

lexical Digits = [0-9]+ !>> [0-9] !>> ",0" !>> ",1" !>> ",2" !>> ",3" !>> ",4" !>> ",5" !>> ",6" !>> ",7" !>> ",8" !>> ",9";

syntax Id​

lexical Id = @category="struct" ([\-0-9A-Za-z] !<< [\-0-9]* [A-Za-z] [\-_0-9A-Za-z]* !>> [\-_0-9A-Za-z]) \ CobwordKeywords;

syntax CobwordKeywords​

keyword CobwordKeywords
= 'BY'
| 'OFF'
| 'REPLACE'
;