Lazarus

Free Pascal => Beginners => Topic started by: Anonimista on September 23, 2019, 01:49:26 pm

Title: Make Plex read from string
Post by: Anonimista on September 23, 2019, 01:49:26 pm
Plex (lexer generator) reads from the standard input or from a file

Code: Pascal  [Select][+][-]
  1. yyinput, yyoutput : Text;        (* input and output file *)

Is there a way to make it read a string? ie I need to read a string as if it was a stream I guess.
Title: Re: Make Plex read from string
Post by: marcov on September 23, 2019, 02:10:59 pm
Streams can be assigned to "text" variables. See Streamio.assignstream
Title: Re: Make Plex read from string
Post by: lucamar on September 23, 2019, 02:57:59 pm
To complete Marco's answer, you can build a stream from a string by using a TStringStream, so it would be a two-step process: creating the stream from the string and then assigning a textfile to that stream with StreamIO.AssignStream.
Title: Re: Make Plex read from string
Post by: Anonimista on September 23, 2019, 02:58:46 pm
Thanks, it should work, here is a small test I did

Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$mode objfpc}{$H+}
  4. uses streamio, classes;
  5.  
  6. var
  7.  
  8.   t: Text;
  9.   s: string;
  10.   stream: TStringStream;
  11.  
  12. begin
  13.  
  14.   stream := TStringStream.Create('abcd');
  15.   assignstream(t, stream);
  16.   reset(t);
  17.   readln(t, s);
  18.   writeln(s);
  19.   readln;
  20.   stream.Free;
  21.  
  22. end.
  23.  
TinyPortal © 2005-2018