Recent

Author Topic: Make Plex read from string  (Read 202 times)

Anonimista

  • New Member
  • *
  • Posts: 19
Make Plex read from string
« 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.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7931
Re: Make Plex read from string
« Reply #1 on: September 23, 2019, 02:10:59 pm »
Streams can be assigned to "text" variables. See Streamio.assignstream

lucamar

  • Hero Member
  • *****
  • Posts: 2405
Re: Make Plex read from string
« Reply #2 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.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.4/2.0.6  - FPC 3.0.4 on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Anonimista

  • New Member
  • *
  • Posts: 19
Re: Make Plex read from string
« Reply #3 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.