Sto scrivendo un parser di codice Delphi usando Parsec, i miei attuali strutture di dati AST simile a questa:Come posso gestire i commenti nel mio AST?
module Text.DelphiParser.Ast where
data TypeName = TypeName String [String] deriving (Show)
type UnitName = String
data ArgumentKind = Const | Var | Out | Normal deriving (Show)
data Argument = Argument ArgumentKind String TypeName deriving (Show)
data MethodFlag = Overload | Override | Reintroduce | Static | StdCall deriving (Show)
data ClassMember =
ConstField String TypeName
| VarField String TypeName
| Property String TypeName String (Maybe String)
| ConstructorMethod String [Argument] [MethodFlag]
| DestructorMethod String [Argument] [MethodFlag]
| ProcMethod String [Argument] [MethodFlag]
| FunMethod String [Argument] TypeName [MethodFlag]
| ClassProcMethod String [Argument] [MethodFlag]
| ClassFunMethod String [Argument] TypeName [MethodFlag]
deriving (Show)
data Visibility = Private | Protected | Public | Published deriving (Show)
data ClassSection = ClassSection Visibility [ClassMember] deriving (Show)
data Class = Class String [ClassSection] deriving (Show)
data Type = ClassType Class deriving (Show)
data Interface = Interface [UnitName] [Type] deriving (Show)
data Implementation = Implementation [UnitName] deriving (Show)
data Unit = Unit String Interface Implementation deriving (Show)
voglio conservare i commenti nei miei strutture dati AST e attualmente sto cercando di capire come fare Questo.
Il mio parser è diviso in un lexer e un parser (entrambi scritti con Parsec) e ho già implementato il lexing dei token dei commenti.
unit SomeUnit;
interface
uses
OtherUnit1, OtherUnit2;
type
// This is my class that does blabla
TMyClass = class
var
FMyAttribute: Integer;
public
procedure SomeProcedure;
{ The constructor takes an argument ... }
constructor Create(const Arg1: Integer);
end;
implementation
end.
Il flusso di token si presenta così:
[..., Type, LineComment " This is my class that does blabla", Identifier "TMyClass", Equals, Class, ...]
Il parser questo si traduce in:
Class "TMyClass" ...
Il tipo di dati Class
non ha alcun modo di allegare commenti e poiché i commenti (in particolare i commenti dei blocchi) potrebbero apparire quasi ovunque nel flusso di token. Dovrei aggiungere un commento facoltativo a tutti i tipi di dati nell'AST?
Come posso gestire i commenti nel mio AST?
vedere anche http://stackoverflow.com/questions/9392546/pretty-print-haskell-source-code-with-comments e verificare come sono definiti questi AST. Ad esempio, http://hackage.haskell.org/package/haskell-src-exts-1.16.0.1/docs/Language-Haskell-Exts-Annotated-Syntax.html è polimorfico nell'annotazione. – d8d0d65b3f7cf42