我正在尝试根据以下接口在 Ada 中对一组解析器进行建模:
package Parsers is
type Parser is interface;
type DataArray is array(Integer range <>) of String(1..100);
function Parse(Object : access Parser; FilePath : String) return DataArray is abstract;
end Parsers;
第一个 Parser 接口成员是一个文本解析器,如下所示:
with Parsers;
package TextParsers is
type Parser is new Parsers.Parser with null record;
overriding function Parse(Object : access Parser; FilePath : String) return Parsers.DataArray;
end TextParsers;
with Parsers;
use Parsers;
package body TextParsers is
overriding function Parse(Object : access Parser; FilePath : String) return Parsers.DataArray is
Data : Parsers.DataArray (0..144);
begin
-- just stubbed out
return Data;
end Parse;
end TextParsers;
最后,我希望有一个工厂方法根据提供的路径创建这些解析器,比如检测它是 txt 文件还是 csv 等。这是工厂代码:
with Parsers;
use Parsers;
package ParserFactories is
function GetParser(Path : String) return Parsers.Parser;
end ParserFactories;
with Parsers, TextParsers;
package body ParserFactories is
function GetParser(Path : String) return Parsers.Parser is
Text : TextParsers.Parser;
Parse : Parsers.Parser'Class := Text;
begin
return Parse;
end GetParser;
end ParserFactories;
我不断收到“不允许动态标记的表达式”编译器错误,我无法弄清楚如何创建这些实现 Parser 接口的对象并将其从该函数中返回。有没有办法在 Ada 中做到这一点?