Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program ConsoleMetrology;
- {$APPTYPE CONSOLE}
- {$R *.res}
- uses
- System.SysUtils, System.RegularExpressions,
- uFiles in 'uFiles.pas';
- const
- N = 1000;
- type
- TRec = record
- id: string[255];
- count: Integer;
- end;
- TArray = array [1 .. N] of TRec;
- const
- stringPattern = '"[^"]*"';
- charPattern = '\x27(.?|\\u[0-9a-fA-F]*)\x27';
- floatPattern = '[0-9]+\.[0-9]+((E|e)?\-?[0-9]+)?';
- numPattern = '\W([0-9]([xX][0-9a-fA-F]*|[bB][0-1]*|[0-9]*))';
- identsPattern = 'null|true|false|([A-Za-z_]\w*)';
- deletePattern = 'public|private|:|(\[ *\])|\)|\}|protected|static|([A-Za-z_]\w*\.)+([A-Za-z_]\w*)?';
- fnDeclarationPattern =
- '(byte|short|int|long|float|double|String|char|boolean|void) +[A-Za-z_]\w* *\(';
- fnPattern = '([A-Za-z_]\w* *\()|break|continue|goto|return';
- bracketsPattern = '\)|\}|\]';
- o2perationsPattern = '(\+\+)|(\-\-)|(\+=)|(\*=)|(\-=)|(<=)|(>=)|(==)|(!=)|(%=)|(/=)|(%=)|(&&)|(\|\|)';
- o1perationsPattern = '[\+\-=\*/&\|!%\~\>\<\,\;\(\{(\[)]';
- typesPattern = '[\W](byte |short |int |long |float |double |String |char |boolean |void )';
- var
- Operators, Operands: TArray;
- s: string;
- StringRE: TRegEx;
- CharRE: TRegEx;
- NumRE: TRegEx;
- DeleteRE: TRegEx;
- OtherLiteralsRE: TRegEx;
- FnDeclarationRE: TRegEx;
- FnRE: TRegEx;
- BracketsRE: TRegEx;
- IdentsRE: TRegEx;
- TypesRE: TRegEx;
- O1perationsRE: TRegEx;
- O2perationsRE: TRegEx;
- FloatRE: TRegEx;
- StringCollection: TMatchCollection;
- CharCollection: TMatchCollection;
- NumCollection: TMatchCollection;
- OtherLiteralsCollection: TMatchCollection;
- FnDeclarationCollection: TMatchCollection;
- FnCollection: TMatchCollection;
- IdentsCollection: TMatchCollection;
- TypesCollection: TMatchCollection;
- O1perationsCollection: TMatchCollection;
- O2perationsCollection: TMatchCollection;
- FloatCollection: TMatchCollection;
- i: Integer;
- buf: string;
- procedure AddToArray(str: string; var arr: TArray);
- var
- i: Integer;
- Flag: Boolean;
- begin
- i := 1;
- Flag := True;
- while Flag and (i <= N) do
- begin
- if arr[i].id = '' then
- begin
- arr[i].id := str;
- arr[i].count := 1;
- Flag := False;
- end
- else if str = arr[i].id then
- begin
- Flag := False;
- Inc(arr[i].count);
- end;
- Inc(i);
- end;
- end;
- begin
- GetCodeFromFile(s);
- StringRE := TRegEx.Create(stringPattern);
- CharRE := TRegEx.Create(charPattern);
- NumRE := TRegEx.Create(numPattern);
- IdentsRE := TRegEx.Create(identsPattern);
- DeleteRE := TRegEx.Create(deletePattern);
- FnDeclarationRE := TRegEx.Create(fnDeclarationPattern);
- FnRE := TRegEx.Create(fnPattern);
- BracketsRE := TRegEx.Create(bracketsPattern);
- IdentsRE := TRegEx.Create(identsPattern);
- TypesRE := TRegEx.Create(typesPattern);
- O2perationsRE := TRegEx.Create(o2perationsPattern);
- O1perationsRE := TRegEx.Create(o1perationsPattern);
- FloatRE := TRegEx.Create(floatPattern);
- // Строковые литералы
- StringCollection := StringRE.Matches(s);
- s := StringRE.Replace(s, ' ');
- // Символьные литералы
- CharCollection := CharRE.Matches(s);
- s := CharRE.Replace(s, ' ');
- // Объявления функций
- FnDeclarationCollection := FnDeclarationRE.Matches(s);
- s := FnDeclarationRE.Replace(s, ' ');
- // Вызовы функций / операторы
- FnCollection := FnRE.Matches(s);
- s := FnRE.Replace(s, ' ');
- s := DeleteRE.Replace(s, '');
- s := BracketsRE.Replace(s, ' ');
- // Знаки операций
- O2perationsCollection := O2perationsRE.Matches(s);
- s := O2perationsRE.Replace(s, ' ');
- O1perationsCollection := O1perationsRE.Matches(s);
- s := O1perationsRE.Replace(s, ' ');
- // Чыселки
- FloatCollection := FloatRE.Matches(s);
- s := FloatRE.Replace(s, ' ');
- NumCollection := NumRE.Matches(s);
- s := NumRE.Replace(s, ' ');
- s := TypesRE.Replace(s, '');
- // Идентификаторы
- IdentsCollection := IdentsRE.Matches(s);
- s := IdentsRE.Replace(s, ' ');
- // Сбор коллекций по порядку!!!
- Writeln('Строки:');
- for i := 0 to StringCollection.count - 1 do
- begin
- Writeln(StringCollection.Item[i].Value);
- AddToArray(StringCollection.Item[i].Value, Operands);
- end;
- Writeln(#10#13'Символы:');
- for i := 0 to CharCollection.count - 1 do
- begin
- Writeln(CharCollection.Item[i].Value);
- AddToArray(CharCollection.Item[i].Value, Operands);
- end;
- // Writeln(#10#13'Другое:');
- for i := 0 to OtherLiteralsCollection.count - 1 do
- begin
- Writeln(OtherLiteralsCollection.Item[i].Value);
- end;
- Writeln(#10#13'Функции (объявление):');
- for i := 0 to FnDeclarationCollection.count - 1 do
- begin
- Writeln(FnDeclarationCollection.Item[i].Value + ')');
- end;
- Writeln(#10#13'Функции (вызов):');
- for i := 0 to FnCollection.count - 1 do
- begin
- if (FnCollection.Item[i].Value <> 'break') and
- (FnCollection.Item[i].Value <> 'continue') and
- (FnCollection.Item[i].Value <> 'goto') then
- begin
- Writeln(FnCollection.Item[i].Value + ')');
- AddToArray(FnCollection.Item[i].Value + ')', Operators);
- end
- else
- begin
- Writeln(FnCollection.Item[i].Value);
- AddToArray(FnCollection.Item[i].Value, Operators);
- end;
- end;
- Writeln(#10#13'Операции:');
- for i := 0 to O2perationsCollection.count - 1 do
- begin
- Writeln(O2perationsCollection.Item[i].Value);
- AddToArray(O2perationsCollection.Item[i].Value, Operators);
- end;
- for i := 0 to O1perationsCollection.count - 1 do
- begin
- Writeln(O1perationsCollection.Item[i].Value);
- AddToArray(O1perationsCollection.Item[i].Value, Operators);
- end;
- Writeln(#10#13'Идентификаторы:');
- for i := 0 to IdentsCollection.count - 1 do
- begin
- Writeln(IdentsCollection.Item[i].Value);
- AddToArray(IdentsCollection.Item[i].Value, Operands);
- end;
- Writeln(#10#13'Числа:');
- for i := 0 to NumCollection.count - 1 do
- begin
- buf := NumCollection.Item[i].Value;
- Delete(buf, 1, 1);
- Writeln(buf);
- AddToArray(buf, Operands);
- end;
- for i := 0 to FloatCollection.count - 1 do
- begin
- Writeln(FloatCollection.Item[i].Value);
- AddToArray(FloatCollection.Item[i].Value, Operands);
- end;
- Readln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement