Jump to content

Abbreviations, simple: Difference between revisions

Ada: Simplify using Find_Token
(Ada version)
(Ada: Simplify using Find_Token)
Line 617:
with Ada.Containers.Vectors;
with Ada.Strings.Fixed;
with Ada.Strings.Maps.Constants;
with Ada.Strings.Unbounded;
with Ada.Text_IoText_IO;
 
procedure Abbreviations_Simple is
Line 624 ⟶ 625:
use Ada.Strings.Unbounded;
subtype Ustring is Unbounded_String;
Empty_String : constant Ustring := Null_Unbounded_String;
 
type Word_Entry is record
Line 631:
end record;
 
package Abbreviation_VectorsCommand_Vectors
is new Ada.Containers.Vectors (Index_Type => Positive,
Element_Type => Word_Entry);
use Abbreviation_Vectors;
 
Commands : Command_Vectors.Vector;
type Scan_State is (Idle, In_Word, After_Word, In_Count);
WordLast_Word : Ustring;
Last_Was_Word : Boolean := False;
 
procedure Append (Word_List : String) is
Abbrev : Vector;
State : Scan_Stateuse := IdleAda.Strings;
Word : Ustring;
Count : Ustring;
 
procedure Append function Is_Word (Word_ListItem : String) return Boolean
is (Fixed.Count (Item, Maps.Constants.Letter_Set) /= 0);
is
 
procedure FlushProcess (Token : String) is
Min : constant Natural := (if Count = ""
then Length (Word)
else Natural'Value (To_String (Count)));
begin
if WordIs_Word /= Empty_String(Token) then
if Last_Was_Word then
Append (Abbrev, Word_Entry'(Word => Word, Min => Min));
Commands.Append ((Word, Char);=> Last_Word,
Min then=> Length (WordLast_Word)));
end caseif;
Last_Word := To_Unbounded_String (Token);
StateLast_Was_Word := IdleTrue;
 
else -- Token is expected to be decimal
Commands.Append ((Word => Last_Word,
Min else=> Natural'Value (To_String (CountToken)));
StateLast_Was_Word := IdleFalse;
end if;
end Word := Empty_StringProcess;
Count := Empty_String;
end Flush;
 
Token_First : Positive := Word_List'First;
Token_Last : Natural;
begin
forwhile CharToken_First ofin 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);
exit when Token_Last = Flush0;
State := In_Word;
when In_Word => null;
end case;
Append (Word, Char);
 
Process (Word_List when '0'(Token_First .. '9' =>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);
 
Token_First := Token_Last when+ ' ' =>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;
 
case State is
when In_Count =>
Flush;
State := Idle;
when In_Word =>
State := After_Word;
when others =>
null;
end case;
end Append;
 
Line 718 ⟶ 689:
end if;
 
for Candidate of AbbrevCommands loop
declare
Upper_Cand : constant String := To_Upper (To_String (Candidate.Word));
Line 743 ⟶ 714:
 
procedure Put_Match (To : String) is
use Ada.Text_IoText_IO;
begin
Put ("Match to '"); Put (To);
Line 761 ⟶ 732:
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 ("copies");
Line 775 ⟶ 747:
{{out}}
<pre>
Match to 'riG' is 'RIGHT'
Match to 'rePEAT' is 'REPEAT'
Match to 'copies' is '*error*'
210

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.