/******************************************************************************/ /* */ /* RVM.RxRegExp.cls - The rxregexp library of the RVM */ /* ================================================== */ /* */ /* ooRexx's rxregexp (extensions/rxregexp) is a library of native methods: */ /* those of the class RegularExpression, which rxregexp.cls (the RVM */ /* compiles it as it is) declares as EXTERNAL "LIBRARY rxregexp RegExp_xxx": */ /* RegExp_Init, RegExp_Uninit, RegExp_Parse, RegExp_Match and RegExp_Pos. It */ /* is a library of the RVM with methods (LibraryMethods, in */ /* RVM.BIFs.cls, has its table; RVM.Objects's LibraryMethod checks their */ /* arguments as the native's and gives the strings to RxRegExpNative). */ /* */ /* The RVM of ooRexx runs the host's rxregexp, as it runs the host's rxmath: */ /* the object variable CSELF of the RegularExpression (the native's pointer */ /* to its automaton) is an object of a class made here with the host's */ /* methods (Method~loadExternalMethod), and each method of the RVM calls its */ /* own; the errors that the native raises (93.0, a wrong match type; 38.0, a */ /* wrong expression in INIT) are raised here, before the host is called. */ /* The object variable !POS (the POSITION of rxregexp.cls) is the host */ /* object's POSITION. When the host has no rxregexp, the library is not */ /* there (98.903). The RVM of JS computes it (js/rxregexp.mjs). */ /* */ /* This file is part of the RVM, the Rexx Virtual Machine */ /* [See https://rexx.epbcn.com/rexx-parser/] */ /* */ /* Copyright (c) 2026 Josep Maria Blasco */ /* */ /* License: Apache License 2.0 (https://www.apache.org/licenses/LICENSE-2.0) */ /* */ /******************************************************************************/ -- The host's automaton: a class with the methods of the host's rxregexp -- (and its POSITION), made once; .Nil when the host has no rxregexp. ::Routine RxRegExpHost Public If .RVM.RxRegExpHost~checked Then Return .RVM.RxRegExpHost~hostClass .RVM.RxRegExpHost~checked = .True cls = .Object~subclass( "RVM.HostRegExp" ) Do name Over "INIT PARSE MATCH POS"~makeArray( " " ) m = .Method~loadExternalMethod( name, "LIBRARY rxregexp RegExp_"name ) If m == .Nil Then Return .Nil cls~define( name, m ) End cls~define( "POSITION", "Expose !POS; Return !POS" ) .RVM.RxRegExpHost~hostClass = cls Return cls ::Class RVM.RxRegExpHost ::Attribute checked Class ::Attribute hostClass Class ::Method activate Class self~checked = .False self~hostClass = .Nil -- Run method 'entry' (an entry of RxRegExpMethods, in RVM.BIFs.cls) with -- 'args' (strings, or .Nil when omitted: checked already); 'pool' has the -- object variables of the receiver (its pool in the scope of the method). ::Routine RxRegExpNative Public Use Strict Arg entry, args, pool key = entry[2] Select Case key When "INIT" Then Do cls = RxRegExpHost() a = .Array~new If args[2] \== .Nil Then Do Call MatchType args[2], .False a[2] = args[2] End h = cls~sendWith( "NEW", a ) -- Without an expression, the native's automaton has not been cleared -- (malloc): in practice, zeros, and it matches anything. The host's may -- have anything: a wrong expression leaves it so (cleared, and state 0 -- an epsilon to 0). If args[1] == .Nil Then h~parse( "", "CURRENT" ) Else If h~parse( args[1], "CURRENT" ) \== 0 Then Raise Syntax 38.0 pool~setValue( "CSELF", h ) Return 0 End When "UNINIT" Then Do pool~setValue( "CSELF", .Nil ) Return 0 End Otherwise Nop End h = pool~valueOf( "CSELF" ) If h == .Nil Then Raise Syntax 93.900 Additional( "RVM: this RegularExpression has no automaton (INIT did not run, or UNINIT did)" ) Select Case key When "PARSE" Then Do type = "CURRENT" If args[2] \== .Nil Then Do Call MatchType args[2], .True type = args[2] End r = h~parse( args[1], type ) End When "MATCH" Then r = h~match( args[1] ) When "POS" Then r = h~pos( args[1] ) End pool~setValue( "!POS", h~position ) Return r -- A match type (strcasecmp): MINIMAL, MAXIMAL, or (PARSE) CURRENT; else 93.0. ::Routine MatchType Use Strict Arg type, current u = Upper( type ) If u == "MINIMAL" | u == "MAXIMAL" Then Return If current, u == "CURRENT" Then Return Raise Syntax 93.0