Skip to content

Commit dca64d3

Browse files
committed
feat (parser): support inline method invocation syntax
Signed-off-by: Saurabh Kumar <developer.saurabh@outlook.com>
1 parent f093a31 commit dca64d3

4 files changed

Lines changed: 66 additions & 15 deletions

File tree

cobc/ChangeLog

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
2026-06-30 Saurabh Kumar <developer.saurabh@outlook.com>
33

44
* parser.y, reserved.c, scanner.l: add support for parsing INTERFACE-ID,
5-
METHOD-ID, FACTORY, OBJECT paragraphs and INVOKE statement. Support
6-
CLASS and INTERFACE specifiers in REPOSITORY paragraph. Also
7-
support keywords SELF and ACTIVE-CLASS.
5+
METHOD-ID, FACTORY, OBJECT paragraphs, INVOKE statement and inline method
6+
invocation expression. Support CLASS and INTERFACE specifiers in REPOSITORY
7+
paragraph. Also support keywords SELF and ACTIVE-CLASS.
88
* tree.h, tree.c, typeck.c: add syntax validation logic to check OO inheritance
99
list for duplicates, consistency with names specified in the REPOSITORY
1010
paragraph

cobc/parser.y

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2709,7 +2709,7 @@ set_oo_class_attr(enum cb_oo_class_attribute attr, const char* attr_name)
27092709
%token CLASS
27102710
%token CLASS_ID "CLASS-ID"
27112711
%token CLASSIFICATION
2712-
%token CLASS_NAME "class-name"
2712+
%token CLASS_NAME
27132713
%token CLEAR_SELECTION "CLEAR-SELECTION"
27142714
%token CLINE
27152715
%token CLINES /* remark: not used here */
@@ -3012,6 +3012,7 @@ set_oo_class_attr(enum cb_oo_class_attribute attr, const char* attr_name)
30123012
%token INITIALIZE
30133013
%token INITIALIZED
30143014
%token INITIATE
3015+
%token INLINE_METHOD_INVOCATION_OP
30153016
%token INPUT
30163017
%token INPUT_OUTPUT "INPUT-OUTPUT"
30173018
%token INQUIRE
@@ -3192,6 +3193,7 @@ set_oo_class_attr(enum cb_oo_class_attribute attr, const char* attr_name)
31923193
%token ONLY
31933194
%token ON_ESCAPE "ON ESCAPE"
31943195
%token ON_EXCEPTION "ON EXCEPTION"
3196+
%token OO_CLASS_NAME "class-name"
31953197
%token OPEN
31963198
%token OPTIONAL
31973199
%token OPTIONS
@@ -4172,7 +4174,7 @@ interface_id_header:
41724174
;
41734175

41744176
class_id_name:
4175-
CLASS_NAME { $$ = $1; }
4177+
OO_CLASS_NAME { $$ = $1; }
41764178
| LITERAL
41774179
{
41784180
cb_trim_program_id ($1);
@@ -8847,7 +8849,7 @@ _object_reference_type:
88478849
/* empty */
88488850
| WORD
88498851
| _factory_of ACTIVE_CLASS
8850-
| _factory_of CLASS_NAME _only
8852+
| _factory_of OO_CLASS_NAME _only
88518853
;
88528854

88538855
_factory_of:
@@ -12217,7 +12219,6 @@ statement:
1221712219
| if_statement
1221812220
| initialize_statement
1221912221
| initiate_statement
12220-
// | inline_method_invocation_statement
1222112222
| inquire_statement
1222212223
| inspect_statement
1222312224
| invoke_statement
@@ -15509,11 +15510,9 @@ id_or_class_name:
1550915510
| class_id_name
1551015511
;
1551115512

15512-
// inline_method_invocation_statement:
15513-
// id_or_class_name "::" literal
15514-
// | id_or_class_name "::" literal
15515-
// TOK_OPEN_PAREN call_param_list TOK_CLOSE_PAREN
15516-
// ;
15513+
inline_method_invocation:
15514+
id_or_class_name INLINE_METHOD_INVOCATION_OP literal func_args
15515+
;
1551715516

1551815517
/* INQUIRE statement */
1551915518

@@ -20637,6 +20636,7 @@ function:
2063720636
{
2063820637
$$ = cb_build_intrinsic ($1, $2, $3, 1);
2063920638
}
20639+
| inline_method_invocation
2064020640
;
2064120641

2064220642
func_no_parm:

cobc/scanner.l

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,7 @@ H#[0-9A-Za-z]+ {
10871087
} else if ((second_last_token == CLASS_ID && last_token == TOK_DOT)
10881088
|| last_token == END_CLASS) {
10891089
yylval = cb_build_reference (yytext);
1090-
RETURN_TOK (CLASS_NAME);
1090+
RETURN_TOK (OO_CLASS_NAME);
10911091
} else if ((second_last_token == METHOD_ID && last_token == TOK_DOT)
10921092
|| last_token == END_METHOD) {
10931093
if (strcasecmp (yytext, "GET") == 0) {
@@ -1283,6 +1283,11 @@ H#[0-9A-Za-z]+ {
12831283
RETURN_TOK (TOK_COLON);
12841284
}
12851285

1286+
"::" {
1287+
yylval = NULL;
1288+
RETURN_TOK (INLINE_METHOD_INVOCATION_OP);
1289+
}
1290+
12861291
"=" {
12871292
yylval = NULL;
12881293
RETURN_TOK (TOK_EQUAL);

tests/testsuite.src/syn_oo.at

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ AT_DATA([prog1.cob], [
5151

5252
AT_CHECK([$COMPILE_ONLY prog1.cob], [1], [],
5353
[prog1.cob:3: error: object-oriented COBOL is not supported
54-
prog1.cob:4: error: syntax error, unexpected ., expecting class-name or Literal
54+
prog1.cob:4: error: syntax error, unexpected ., expecting Literal or class-name
5555
])
5656

5757
AT_DATA([prog2.cob], [
@@ -636,4 +636,50 @@ prog.cob:99: error: 'account-balance' is not defined
636636
prog.cob:100: error: 'account-number' is not defined
637637
prog.cob:101: error: 'the-date' is not defined
638638
])
639-
AT_CLEANUP
639+
AT_CLEANUP
640+
641+
642+
AT_SETUP([Inline Method Invocation Expression])
643+
AT_KEYWORDS([OOP])
644+
645+
AT_DATA([prog.cob], [
646+
IDENTIFICATION DIVISION.
647+
CLASS-ID. MyBaseClass.
648+
FACTORY.
649+
PROCEDURE DIVISION.
650+
METHOD-ID. MyMethod IS FINAL.
651+
DATA DIVISION.
652+
LINKAGE SECTION.
653+
01 VAR-IN PIC 9(2).
654+
01 VAR-OUT PIC 9(2).
655+
PROCEDURE DIVISION USING VAR-IN
656+
RETURNING VAR-OUT.
657+
MOVE VAR-IN TO VAR-OUT.
658+
DISPLAY "Hello, world!".
659+
END METHOD MyMethod.
660+
END FACTORY.
661+
END CLASS MyBaseClass.
662+
663+
PROGRAM-ID. prog.
664+
ENVIRONMENT DIVISION.
665+
CONFIGURATION SECTION.
666+
REPOSITORY.
667+
CLASS MyBaseClass AS "MyClass".
668+
DATA DIVISION.
669+
WORKING-STORAGE SECTION.
670+
01 an-object USAGE OBJECT REFERENCE MyBaseClass.
671+
01 a-var PIC 9(2).
672+
673+
PROCEDURE DIVISION.
674+
DISPLAY an-object :: "MyMethod" (10).
675+
DISPLAY an-object :: "MyMethod" (10 + 2).
676+
MOVE 98 TO a-var.
677+
DISPLAY an-object :: "MyMethod" (a-var).
678+
END PROGRAM prog.
679+
])
680+
681+
AT_CHECK([$COMPILE_ONLY prog.cob], [1], [],
682+
[prog.cob:3: error: object-oriented COBOL is not supported
683+
prog.cob:26: warning: USAGE OBJECT REFERENCE is not implemented
684+
])
685+
AT_CLEANUP

0 commit comments

Comments
 (0)