Abbreviations, simple: Difference between revisions

Content added Content deleted
(Ada version)
(Ada: Simplify using Find_Token)
Line 617: Line 617:
with Ada.Containers.Vectors;
with Ada.Containers.Vectors;
with Ada.Strings.Fixed;
with Ada.Strings.Fixed;
with Ada.Strings.Maps.Constants;
with Ada.Strings.Unbounded;
with Ada.Strings.Unbounded;
with Ada.Text_Io;
with Ada.Text_IO;


procedure Abbreviations_Simple is
procedure Abbreviations_Simple is
Line 624: Line 625:
use Ada.Strings.Unbounded;
use Ada.Strings.Unbounded;
subtype Ustring is Unbounded_String;
subtype Ustring is Unbounded_String;
Empty_String : constant Ustring := Null_Unbounded_String;


type Word_Entry is record
type Word_Entry is record
Line 631: Line 631:
end record;
end record;


package Abbreviation_Vectors
package Command_Vectors
is new Ada.Containers.Vectors (Index_Type => Positive,
is new Ada.Containers.Vectors (Index_Type => Positive,
Element_Type => Word_Entry);
Element_Type => Word_Entry);
use Abbreviation_Vectors;


Commands : Command_Vectors.Vector;
type Scan_State is (Idle, In_Word, After_Word, In_Count);
Last_Word : Ustring;
Last_Was_Word : Boolean := False;


procedure Append (Word_List : String) is
Abbrev : Vector;
State : Scan_State := Idle;
use Ada.Strings;
Word : Ustring;
Count : Ustring;


procedure Append (Word_List : String)
function Is_Word (Item : String) return Boolean
is (Fixed.Count (Item, Maps.Constants.Letter_Set) /= 0);
is

procedure Flush is
procedure Process (Token : String) is
Min : constant Natural := (if Count = ""
then Length (Word)
else Natural'Value (To_String (Count)));
begin
begin
if Word /= Empty_String then
if Is_Word (Token) then
if Last_Was_Word then
Append (Abbrev, Word_Entry'(Word => Word, Min => Min));
Commands.Append ((Word => Last_Word,
Min => Length (Last_Word)));
end if;
Last_Word := To_Unbounded_String (Token);
Last_Was_Word := True;

else -- Token is expected to be decimal
Commands.Append ((Word => Last_Word,
Min => Natural'Value (Token)));
Last_Was_Word := False;
end if;
end if;
Word := Empty_String;
end Process;
Count := Empty_String;
end Flush;


Token_First : Positive := Word_List'First;
Token_Last : Natural;
begin
begin
for Char of Word_List loop
while Token_First in Word_List'Range loop
case Char is


Fixed.Find_Token (Word_List, Maps.Constants.Alphanumeric_Set,
when 'a' .. 'z' | 'A' .. 'Z' =>
case State is
Token_First, Inside,
when Idle | In_Count | After_Word =>
Token_First, Token_Last);
Flush;
exit when Token_Last = 0;
State := In_Word;
when In_Word => null;
end case;
Append (Word, Char);


when '0' .. '9' =>
Process (Word_List (Token_First .. Token_Last));
case State is
when Idle | In_Word =>
State := In_Count;
when In_Count => null;
when After_Word =>
State := In_Count;
Count := Empty_String;
end case;
Append (Count, Char);


when ' ' =>
Token_First := Token_Last + 1;
case State is
when In_Word =>
State := After_Word;
when In_Count =>
Flush;
State := Idle;
when Idle | After_Word => null;
end case;

when others =>
raise Constraint_Error with "unexpected input";
end case;
end loop;
end loop;

case State is
when In_Count =>
Flush;
State := Idle;
when In_Word =>
State := After_Word;
when others =>
null;
end case;
end Append;
end Append;


Line 718: Line 689:
end if;
end if;


for Candidate of Abbrev loop
for Candidate of Commands loop
declare
declare
Upper_Cand : constant String := To_Upper (To_String (Candidate.Word));
Upper_Cand : constant String := To_Upper (To_String (Candidate.Word));
Line 743: Line 714:


procedure Put_Match (To : String) is
procedure Put_Match (To : String) is
use Ada.Text_Io;
use Ada.Text_IO;
begin
begin
Put ("Match to '"); Put (To);
Put ("Match to '"); Put (To);
Line 761: Line 732:
A ("2 save set shift 2 si sort sos stack 3 status 4 top transfer 3 type 1 up 1");
A ("2 save set shift 2 si sort sos stack 3 status 4 top transfer 3 type 1 up 1");


Put_Match ("riG");
Put_Match ("rePEAT");
Put_Match ("rePEAT");
Put_Match ("copies");
Put_Match ("copies");
Line 775: Line 747:
{{out}}
{{out}}
<pre>
<pre>
Match to 'riG' is 'RIGHT'
Match to 'rePEAT' is 'REPEAT'
Match to 'rePEAT' is 'REPEAT'
Match to 'copies' is '*error*'
Match to 'copies' is '*error*'