base_scanner.pas

来自「Delphi脚本控件」· PAS 代码 · 共 1,577 行 · 第 1/3 页

PAS
1,577 行
字号
          end;
          't':
          begin
            GetNextChar;
            Text := Text + #$09;
          end;
          'n':
          begin
            GetNextChar;
            Text := Text + #$0A;
          end;
          'v':
          begin
            GetNextChar;
            Text := Text + #$0B;
          end;
          'f':
          begin
            GetNextChar;
            Text := Text + #$0C;
          end;
          'r':
          begin
            GetNextChar;
            Text := Text + #$0D;
          end;
          '\':
          begin
            GetNextChar;
            Text := Text + #$5C;
          end;

          else
           Text := Text + c;
          end;
        end
        else
          Text := Text + c;
        '''':
        begin
          Inc(K1);
          Text := Text + c;
        end;
        '"':
        begin
          Inc(K2);
          Text := Text + c;
        end;
        '<':
        if LA(1) = '?' then
        begin
          Dec(P);
          Break;
        end
        else if LA(1) = '%' then
        begin
          Dec(P);
          Break;
        end
        else
          Text := Text + c;
        #255:
          Break;
        else
          Text := Text + c;
      end;
    until false;
  end;
end;

procedure TPAXScanner.ScanString(ch: Char);
var
  LongStr: Boolean;
  Backslash: Boolean;
  S: String;
  I64: Int64;
begin
  with Token do
  begin
    TokenClass := tcStringConst;

    if scripter <> nil then
    begin
      LongStr := (LA(1) = ch) and (LA(2) = ch) and
         TPaxBaseScripter(Scripter).fLongStrLiterals;
      Backslash := TPAXParser(Parser).Backslash;
    end
    else
    begin
      LongStr := false;
      Backslash := false;
    end;

    Text := '';
    repeat
      if P >= Length(Buff) then
          raise TPAXScriptFailure.Create(errInvalidString);

      GetNextChar;

      if (c = '\') and (ch in ['"', '''']) and Backslash then
        case LA(1) of
          '%': ScanFormatString;
          'b':
          begin
            GetNextChar;
            Text := Text + #$08;
          end;
          't':
          begin
            GetNextChar;
            Text := Text + #$09;
          end;
          'n':
          begin
            GetNextChar;
            Text := Text + #$0A;
          end;
          'v':
          begin
            GetNextChar;
            Text := Text + #$0B;
          end;
          'f':
          begin
            GetNextChar;
            Text := Text + #$0C;
          end;
          '''':
          begin
            GetNextChar;
            Text := Text + '''';
          end;
          '"':
          begin
            GetNextChar;
            Text := Text + '"';
          end;
          'r':
          begin
            GetNextChar;
            Text := Text + #$0D;
          end;
          'x': // hex const
          begin
            GetNextChar;
            GetNextChar;
            S := c;
            while LA(1) in ['0'..'9', 'A'..'F', 'a'..'f'] do
              S := S + GetNextChar;

            I64 := StrToInt64('$' + S);
            if I64 > 255 then
              raise TPAXScriptFailure.Create(errInvalidEscapeSequence);

            Text := Text + Chr(I64);
          end;
          '\':
          begin
            GetNextChar;
            Text := Text + #$5C;
          end;
          else
          begin
//            Text := Text + c;
          end;
        end
      else if c = ch then
      begin
        if LongStr then
        begin
          if (LA(1) = ch) and (LA(2) = ch) then
          begin
            GetNextChar;
            GetNextChar;
            c := #0;
            Break;
          end;
        end
        else
        begin
          if LA(1) = ch then
          begin
            Text := Text + GetNextChar;
            c := #0;
          end
          else
            Break;
        end;
      end
      else if c in [#0,#13,#10] then
      begin
        if LongStr then
        begin
          Text := Text + c;
          if c = #13 then
          begin
            c := GetNextChar;
            Text := Text + c;
          end;

          IncLineNumber;
          PosNumber := -1;

          if scripter <> nil then
          with TPAXBaseScripter(Scripter).Code do
            Add(OP_SEPARATOR, ModuleID, LineNumber, 0);
        end
        else
          raise TPAXScriptFailure.Create(errInvalidString);
      end
      else
        Text := Text + c;
    until false;
  end;
  Token.Value := Token.Text;
  SetScannerState(scanProg);
end;

procedure TPAXScanner.ScanCondDir(Start1: Char;
                                  Start2: TCharSet);
label
  NextComment, Fin;
const
  IdsSet = ['a'..'z','A'..'Z','0'..'9','_'];
var
  S: String;
  I, J, J1, J2: Integer;
  Visible: Boolean;
  FileName: String;
  L: TStringList;
begin
  Visible := true;

NextComment:

  S := '';
  repeat
    GetNextChar;
    S := S + c;
    if c in [#10,#13] then
    begin
      IncLineNumber;
      PosNumber := -1;

      if c = #13 then
        GetNextChar;
    end;
  until not (c in (IdsSet + Start2));

  I := Pos('INCLUDE', UpperCase(S));
  if I = 0 then
    I := Pos('I ', UpperCase(S));

  if I > 0 then
  begin
    while not (c in IdsSet) do GetNextChar;
    ScanIdentifierEx(['\', ':']);

    FileName := Token.Text;

    if Pos('\', FileName) > 1 then
      if Pos(':', FileName) = 0 then
        FileName := '\' + FileName;

    if LA(1) = '.' then
    begin
      GetNextChar;
      ScanIdentifier;
      FileName := FileName + Token.Text;
    end;

    if Assigned(TPaxBaseScripter(Scripter).OnInclude) then
    begin
      S := '';
      TPaxBaseScripter(Scripter).OnInclude(TPaxBaseScripter(Scripter).Owner, FileName, S);
    end
    else
    begin
      if Pos('.', FileName) = 0 then
        if TPaxParser(Parser).IncludeFileExt <> '' then
          FileName := FileName + TPaxParser(Parser).IncludeFileExt;

      S := TPaxBaseScripter(Scripter).FindFullName(FileName);
    end;

    if S = '' then
    begin
      raise TPaxScriptFailure.Create(Format(errFileNotFound, [FileName]));
    end
    else if not Assigned(TPaxBaseScripter(Scripter).OnInclude) then
    begin
      FileName := S;

      L := TStringList.Create;
      L.LoadFromFile(FileName);
      S := L.Text;
      L.Free;
    end;

    if LA(1) = '}' then
      GetNextChar;
    if LA(1) = '"' then
      GetNextChar;
    if LA(1) = '''' then
      GetNextChar;

    ScannerStack.Push(Self);

    P := 0;
    Buff := S + #255#255#255;
    LineNumber := 1;
    PosNumber := 0;
    fScannerState := scanProg;

    Exit;
  end;

  I := Pos('DEFINE', UpperCase(S));
  if I > 0 then
  begin
    while not (c in IdsSet) do GetNextChar;
    ScanChars(['A'..'Z', 'a'..'z', '0'..'9', '_', '.', '-', '[', ']', '(', ')', ',']);
    Token.Text := UpperCase(Token.Text);

    TPaxBaseScripter(Scripter).DefList.Add(Token.Text);

    if StrEql(Token.Text, 'DECLARE_ON') then
      TPaxBaseScripter(Scripter).Code.Add(OP_DECLARE_ON, 0, 0, 0)
    else if StrEql(Token.Text, 'DECLARE_OFF') then
      TPaxBaseScripter(Scripter).Code.Add(OP_DECLARE_OFF, 0, 0, 0)
    else if StrEql(Token.Text, 'UPCASE_ON') then
      TPaxBaseScripter(Scripter).Code.Add(OP_UPCASE_ON, 0, 0, 0)
    else if StrEql(Token.Text, 'UPCASE_OFF') then
      TPaxBaseScripter(Scripter).Code.Add(OP_UPCASE_OFF, 0, 0, 0)
    else if StrEql(Token.Text, 'OPTIMIZATION_ON') then
      TPaxBaseScripter(Scripter).Code.Add(OP_OPTIMIZATION_ON, 0, 0, 0)
    else if StrEql(Token.Text, 'OPTIMIZATION_OFF') then
      TPaxBaseScripter(Scripter).Code.Add(OP_OPTIMIZATION_OFF, 0, 0, 0);

    with TPaxBaseScripter(Scripter) do
    begin
      if Assigned(OnDefine) then
        OnDefine(Owner, Token.Text);
      Code.Add(OP_DEFINE, SymbolTable.AppVariantConst(Token.Text), 0, 0);
    end;

    if LA(1) = '}' then
      GetNextChar;

    Exit;
  end;

  I := Pos('UNDEF', UpperCase(S));
  if I > 0 then
  begin
    while not (c in IdsSet) do GetNextChar;
    ScanIdentifier;
    Token.Text := UpperCase(Token.Text);

    I := TPaxBaseScripter(Scripter).DefList.IndexOf(Token.Text);
    if I <> -1 then
      TPaxBaseScripter(Scripter).DefList.Delete(I);

    if LA(1) = '}' then
      GetNextChar;

    Exit;
  end;

  I := Pos('IFDEF', UpperCase(S));
  if I > 0 then
  begin
    while not (c in IdsSet) do GetNextChar;
    ScanIdentifier;
    Token.Text := UpperCase(Token.Text);

    Visible := TPaxBaseScripter(Scripter).DefList.IndexOf(Token.Text) <> -1;
    DefStack.Push(_IFDEF, Token.Text, Visible);

    if Visible then
      for I := DefStack.Count - 1 downto 1 do
        if DefStack[I].Word in [_IFDEF, _IFNDEF] then
        begin
          Visible := DefStack[I].Vis;
          Break;
        end;

    if LA(1) = '}' then
      GetNextChar;

    goto Fin;
  end;

  I := Pos('IFNDEF', UpperCase(S));
  if I > 0 then
  begin
    while not (c in IdsSet) do GetNextChar;
    ScanIdentifier;
    Token.Text := UpperCase(Token.Text);

    Visible := TPaxBaseScripter(Scripter).DefList.IndexOf(Token.Text) = -1;
    DefStack.Push(_IFNDEF, Token.Text, Visible);

    if Visible then
      for I := DefStack.Count - 1 downto 1 do
        if DefStack[I].Word in [_IFDEF, _IFNDEF] then
        begin
          Visible := DefStack[I].Vis;
          Break;
        end;

    if LA(1) = '}' then
      GetNextChar;

    goto Fin;
  end;

  I := Pos('ELSE', UpperCase(S));
  if I > 0 then
  begin
    if LA(1) = '}' then
      GetNextChar;

    DefStack.Push(_ELSE, '', Visible);

    I := DefStack.Count - 1;
    if I <= 0 then
      raise TPaxScriptFailure.Create(errInvalidCompilerDirective);

    if DefStack[I].Word in [_IFDEF, _IFNDEF] then
    begin
      DefStack[DefStack.Count].What := DefStack[I].What;
      DefStack[DefStack.Count].Vis := not DefStack[I].Vis;

      Visible := DefStack[DefStack.Count].Vis;
      if Visible then
        for J:=I - 1 downto 1 do
          if DefStack[J].Word in [_IFDEF, _IFNDEF] then
          begin
            Visible := DefStack[J].Vis;
            Break;
          end;
    end
    else
      raise TPaxScriptFailure.Create(errInvalidCompilerDirective);

    goto Fin;
  end;

  I := Pos('ENDIF', UpperCase(S));
  if I > 0 then
  begin
    if LA(1) = '}' then
      GetNextChar;

    J1 := 0;
    J2 := 0;
    for I := DefStack.Count downto 1 do
      if DefStack[I].Word in [_IFDEF, _IFNDEF] then
        Inc(J1)
      else if DefStack[I].Word = _ENDIF then
        Inc(J2);
    if J2 >= J1 then
      raise TPaxScriptFailure.Create(errInvalidCompilerDirective);

    for I:=DefStack.Count downto 1 do
      if DefStack[I].Word in [_IFDEF, _IFNDEF] then
      begin
        while DefStack.Count > I - 1 do
          DefStack.Pop;
        Break;
      end;
    if DefStack.Count = 0 then
      Visible := true
    else
      Visible := DefStack[DefStack.Count].Vis;

    goto Fin;
  end;

  raise TPaxScriptFailure.Create(errInvalidCompilerDirective);

Fin:

  if not Visible then
  begin

    repeat
      GetNextChar;
      if c in [#10,#13] then
      begin
        IncLineNumber;
        PosNumber := -1;

        if c = #13 then
          GetNextChar;
      end;
    until ((c = Start1) and (LA(1) in Start2)) or IsEOF;

    if IsEOF then
      raise TPaxScriptFailure.Create(errMissingENDIFdirective);

    goto NextComment;
  end;
end;

function TPAXScanner.IsEOF: Boolean;
begin
  result := c = #255;
end;

procedure TPAXScanner.IncLineNumber;
begin
  if ScannerStack.Count = 0 then
  begin
    Inc(LineNumber);
    Inc(TPAXBaseScripter(Scripter).fTotalLineCount);
  end;
end;

end.


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?