Delphi XE5教程11:Tokens

来源:互联网 时间:1970-01-01

内容源自Delphi XE5 UPDATE 2官方帮助《Delphi Reference》,本人水平有限,欢迎各位高人修正相关错误!
也欢迎各位加入到Delphi学习资料汉化中来,有兴趣者可QQ:[email protected]

2 Tokens

2 Tokens

 

On the simplest level, a program is a sequence of tokens delimited by separators. A token is the smallest meaningful unit of text in a program. A separator is either a blank or a comment. Strictly speaking, it is not always necessary to place a separator between two tokens; for example, the code fragment:

在最简单层次上,一个程序是一系列由分隔符隔开的 token 构成的。在一个程序中,token 是有意义的最小文字单元,分隔符可以是空白符,也可以是注释。严格说来,并不是任何时候在两个token 之间都要有一个分隔符。如下面的代码:

 

 Size:=20;Price:=10;

 

 

is perfectly legal. Convention and readability, however, dictate that we write this in two lines, as:

是完全合法的。但为遵循约定和代码的可读性起见,我们应当如下书写代码:

 

 Size := 20;

Price := 10;

 

 

Tokens are categorized as special symbols, identifiers, reserved words, directives, numerals, labels, and character strings. A separator can be part of a token only if the token is a character string. Adjacent identifiers, reserved words, numerals, and labels must have one or more separators between them.

Token 被分为特殊符号、标识符、关键字(保留字)、指示字、数字、标签和字符串(常量字符串)。只有当token 是常量字符串时,它才可以包含分隔符。紧邻的标识符、保留字、数字和标签,它们之间必须有一个或多个分隔符。

 

2.1 Special Symbols

特殊符号

 

Special symbols are non-alphanumeric characters, or pairs of such characters, that have fixed meanings. The following single characters are special symbols:

特殊符号是非文字和数字字符,或这类字符的组合,它们有固定的意义。以下是单字符的特殊符号:

 

# $ & ' ( ) * + , - . / : ; < = > @ [ ] ^ { }

 

The following character pairs are also special symbols:

以下的组合字符也是特殊符号:

(* (. *) .) .. // := <= >= <>

 

The following table shows pairs of symbols used in Delphi that have similar meanings (the symbol pairs {} and (* *) are comment characters that are further described in Comments and Compiler Directives):

下表显示在Delphi中有相似含义的符号(符号{}和(* *)是注释符,会在编译器编译时进一步在注释里描述):

                       

 

 The left bracket [ is similar to the character pair of left parenthesis and period (..

 

The right bracket ] is similar to the character pair of period and right parenthesis .).

 

The left brace { is similar to the character pair of left parenthesis and asterisk (*.

 

The right brace } is similar to the character pair of asterisk and right parenthesis *).

上面,‘[’相当于‘(.’,‘]’相当于‘.)’;‘(*’和‘*)’分别相当于‘{’和‘}’(表示注释)。

 

 

Note: %, ?, \, !, " (double quotation marks), _ (underscore), | (pipe), and ~ (tilde) are not special symbols.

请注意,!(惊叹号)、 ”(双引号)、 %(百分号)、 ?(问号)、 \(反斜杠)、 _(下划线)、 |(通道)和 ~(破折号)不是特殊符号。

 

2.2 Identifiers

 

Identifiers denote constants, variables, fields, types, properties, procedures, functions, programs, units, libraries, and packages. An identifier can be of any length, but only the first 255 characters are significant. An identifier must begin with an alphabetic character, a Unicode character, or an underscore (_) and cannot contain spaces. Alphanumeric characters, Unicode characters, digits, and underscores are allowed after the first character. Reserved words cannot be used as identifiers. Since the Delphi Language is case-insensitive, an identifier like CalculateValue could be written in any of these ways:

标识符用来表示常量、变量、字段、类型、属性、过程、函数、程序、单元、库以及包。一个标识符的长度是任意的,但只有前面的255 个字符是有意义的。标志符必须以字母或下划线(_)开始,后面可以是字母、数字和下划线,但不能包含空格。关键字不能用作标识符。因为 Object Pascal 是不区分大小写的,所以,象CalculateValue 标志符,它可以是下面的任何形式:

 CalculateValue

 calculateValue

 calculatevalue

 CALCULATEVALUE

 

Since unit names correspond to file names, inconsistencies in case can sometimes affect compilation. For more information, see the section Unit References and the Uses Clause in Programs and Units.

因为单元名称对应于文件名,在不一致的情况下,有时会影响编译。欲了解更多信息,请参阅单元引用和uses子句中的程序和单元。

 

2.2.1 Qualified Identifiers

2.2.1限定符

 

When you use an identifier that has been declared in more than one place, it is sometimes necessary to qualify the identifier. The syntax for a qualified identifier is:

当一个标识符(名称相同)在多个地方声明时,使用它时可能要对标识符进行限定。限定标识符的语法为:

 

identifier1.identifier2

 

 

where identifier1 qualifies identifier2. For example, if two units each declare a variable called CurrentValue, you can specify that you want to access the CurrentValue in Unit2 by writing:

这里,identifier1 限定identifier2。比如,若两个单元分别声明了一个叫做CurrentValue 的变量,你可以通过如下方式指定要使用Unit2 单元的CurrentValue:

 

 Unit2.CurrentValue

 

 

Qualifiers can be iterated. For example:

限定符可以重复,比如

 

 Form1.Button1.Click

 

 

calls the Click method in Button1 of Form1.

它调用Form1 中Button1 的Click 方法。

 

If you don't qualify an identifier, its interpretation is determined by the rules of scope described in Blocks and scope inside Declarations and Statements.

如果你没有使用限定符,在块和范围一节中所讲述的范围规则将决定它作如何解释。

 

2.2.2 Extended Identifiers

2.2.2 扩展标识符

 

You might encounter identifiers (e.g. types, or methods in a class) having the same name as a Delphi language reserved word. For example, a class might have a method called begin. Delphi reserved words such as begin cannot be used for an identifier name.

您可能会遇到的标识符(如类型,或在类中的方法)和Delphi语言的一个保留字具有相同的名称 。例如,一个类可能有一个名为begin方法,而Delphi 保留字begin不能用作其标识符名称。

 

If you fully qualify the identifier, then there is no problem. For example, if you want to use the Delphi reserved word type for an identifer name, you must use its fully qualified name:

如果你完全限定标识符,那么就没有问题。例如,如果你想使用一个Delphi保留字作为标识符名称类型,则必须使用完全限定名称:

 

 var TMyType.type

// Using a fully qualified name avoids ambiguity with {{Delphi}} language keyword.

/ /使用一个完全限定标识符避免{ { Delphi }}语言关键字歧义。

 

 

As a shorter alternative, the ampersand (&) operator can be used to resolve ambiguities between identifiers and Delphi language reserved words. The & prevents a keyword from being parsed as a keyword (that is, a reserved word). If you encounter a method or type that is the same name as a Delphi keyword, you can omit the namespace specification if you prefix the identifier name with an ampersand. But when you are declaring an identifier that has the same name as a keyword, you must use the &:

作为一个短替代,符号(& )运算符可以用来解决标识符和Delphi语言的保留字之间的歧义。&符号防止与关键字被解析为一个关键字(即保留字)。如果你遇到一个方法或类型与Delphi关键字名称相同,你可以,如果你有一个&前缀的标识符名称,就可以忽略命名空间规范。但是当你声明和关键字具有相同名称的标识符时,则必须使用& :

 

 type

&Type = Integer;

// Prefix with '&' is ok.

 

 

2.3 Reserved Words

2.3 保留字

 

The following reserved words cannot be redefined or used as identifiers.

下面的保留字不能被重新定义或用作标识符:

Delphi Reserved Words:

Delphi 保留字(64个):

and

end

interface

record

var

array

except

is

repeat

while  

as

exports

label

resourcestring      

with          

asm

file

library

set    

xor

begin

finalization   

mod

shl

 

case

finally

nil

shr

 

class

for

not

string

 

const

function

object

then

 

constructor

goto

of

threadvar

 

destructor

if

or

to

 

dispinterface       

implementation      

packed

try

 

div

in

procedure    

type

 

do

inherited

program        

unit

 

downto

initialization   

property

until

 

else

inline

raise

uses

 

 

Note: In addition to the words in the preceding table, private, protected, public, published, and automated act as reserved words within class type declarations, but are otherwise treated as directives. The words at and on also have special meanings, and should be treated as reserved words. The keywords of object are used to define method pointers.

注:除上面的关键字外,private、protected、public、published和automated在对象类型的声明中用作关键字,但其它情况下则作为指示字。关键字at和on也具有特殊的含义,将作为指示字。这些对象的关键字,用于定义方法的指针。

 

2.4 Directives

2.4指示字

 

Delphi has more than one type of directive. One meaning for "directive' is a word that is sensitive in specific locations within source code. This type of directive has special meaning in the Delphi language, but, unlike a reserved word, appears only in contexts where user-defined identifiers cannot occur. Hence -- although it is inadvisable to do so -- you can define an identifier that looks exactly like a directive.

Delphi有不止一种类型的指示字。在源代码中一个特定位置的指示字代表一个意思。在Delphi中,指示字具有特殊的意义,但和关键字不同,它只用于用户-自定义标识符不能出现的上下文环境中。因此,你可以定义一个和指示字完全相同的标志符,虽然这是不明智的。

 

Directives:

指示字(57个):

absolute

export

name

public

stdcall

abstract

external

near1

published

strict

assembler

far1

nodefault

read

stored

automated

final

operator10

readonly

unsafe

cdecl

forward

out

reference9

varargs

contains7

helper8

overload

register

virtual

default

implements

override7

reintroduce

winapi6

delayed11

index

package

requires7

write

deprecated

inline2

pascal

resident1

writeonly

dispid

library3

platform

safecall

 

dynamic

local4

private

sealed5

 

experimental

message

protected

static

 

 

Notes: 

注:

1. far, near, and resident are obsolete. 

1. far, near, 和 resident 是已经过时的。

 

2. inline is used directive-style at the end of procedure and function declaration to mark the procedure or function for inlining, but became a reserved word for Turbo Pascal. 

2.inline内联函数是用于过程和函数声明的指示字-类型,用于标记该inline内联函数的过程或函数结束,但在Turbo Pascal中它变成了保留字。

 

3. library is also a keyword when used as the first token in project source code; it indicates a DLL target. Otherwise, it marks a symbol so that it produces a library warning when used. 

3.library也是在项目源代码中被当作关键字使用的第一个token,它表示一个DLL的目标。否则,它标志着产生一个警告库时的一个符号。

 

4. local was a Kylix directive and is ignored for Delphi for Win32. 

4.local是Kylix的指令和在Win32中已经被Delphi忽略。

 

5. sealed is a class directive with odd syntax: 'class sealed'. A sealed class cannot be extended or derived (like final in C++).

5.sealed密封类是一个类指示字,并且有奇怪的语法 'class sealed“。密封类不能扩展或派生(类似于C++中的final)。

 

6. winapi is the same as stdcall for Delphi for Win32; 64-bit is different.

6.winapi和win32中Delphi的stdcall是一样的; 64位是不同的。

 

7. package, when used as the first token, indicates a package target and enables package syntax. requires and contains are directives only in package syntax.

7.package,当被用于第一个token时,表示包的目标,并启用包语法。Requirescontains仅在package中是指示字

 

8. helper indicates "class helper for." 

8.helper表示” class helper for.” 。

 

9. reference indicates a reference to a function or procedure.

9.reference表示引用的函数或过程。

 

10. operator indicates class operator.

10.operator表示类操作符。

 

11. The delayed directive is described in Libraries and Packages.

11.deplay指令是库和包的描述。

 

For more information about Delphi directives, see:

有关Delphi指示字的更多信息,请查看:

Declarations and Statements#Hinting Directives

#隐示指示字的声明和语句

Delphi methods warning directives

Delphi方法警告指示字

 

2.4.1 Other Types of Directives

2.4.1 其它类型的指示字

 

Delphi has two types of directives, including the context-sensitive type of directive described above.

Delphi有两种类型的指示字,包含在上述指示字描述的相关上下文敏感类型中。

 

A directive can be an identifier -- not typically a keyword -- that you place at the end of a declaration to modify the meaning of the declaration. For example:

一个指令可以是一个标识符 - 通常不是一个关键字 - 您将在声明的最后修改声明的含义。例如:

 

 procedure P; forward;

 

or:

或者:

 

 procedure M; virtual; override;

 

 

or:

或者:

 

 

 property Foo: Integer read FFoo write FFoo default 42;

 

 

 

 The last type of directive is the official compiler directive, which is a switch or option that affects the behavior of the compiler. A compiler directive is surrounded by braces ({}) and begins with a dollar-sign ($), like this:

指令的最后一个类型是官方的编译器指令,这是一个影响编译器行为的开关或选项。编译器指令是用括号括起来({}),并开始于一个美元符号($),像这样:

 

  {$POINTERMATH ON}

 

  {$D+} // DEBUGINFO ON

 

Like the other types of directives, compiler directives are not keywords. For a list of the compiler directives, see the Delphi compiler directives list.

像其他类型的指令一样,编译器指令不是关键字。对于编译器指令的列表,请参见Delphi编译器指令列表。

 

2.5 Numerals

2.5 数字

 

Integer and real constants can be represented in decimal notation as sequences of digits without commas or spaces, and prefixed with the + or - operator to indicate sign. Values default to positive (so that, for example, 67258 is equivalent to +67258) and must be within the range of the largest predefined real or integer type.

整数和实数常量可以用十进制的阿拉伯数字序列来表示,数字之间不能有逗号或空格,但它可以有一个前缀“+”或“-”来表示正负。它的数值默认为正(所以,67258和+67258是相等的),并且必须位于预先定义的实数或整数类型的最大值之内。

 

Numerals with decimal points or exponents denote reals, while other numerals denote integers. When the character E or e occurs within a real, it means "times ten to the power of". For example, 7E2 means 7 * 10^2, and 12.25e+6 and 12.25e6 both mean 12.25 * 10^6.

带有小数点或指数符号的数字表示实数,而其它数字表示整数。当E或e出现在实数中时,它表示10的几次方。比如,7E-2表示7*10^-2,12.25e+6和12.25e6都表示12.25*10^6。

 

The dollar-sign prefix indicates a hexadecimal numeral, for example, $8F. Hexadecimal numbers without a preceding - unary operator are taken to be positive values. During an assignment, if a hexadecimal value lies outside the range of the receiving type an error is raised, except in the case of the Integer (32-bit integer) where a warning is raised. In this case, values exceeding the positive range for Integer are taken to be negative numbers in a manner consistent with 2's complement integer representation.

$前缀表示一个16进制数字,比如$8F。没有“-”号运算符的数字被认为是正数。在赋值时,若它的值在接收者的数据类型范围之外,将产生一个错误,除非它是整数类型(32 位整数),此时将引发一个警告。在这种情况下,超出范围的正整数值将取为负数,与2的补码整数表示方式一致。

 

For more information about real and integer types, see Data Types, Variables, and Constants. For information about the data types of numerals, see Declared Constants.

关于实数和整数类型的更多信息,请参考数据类型、变量和常量。关于数字常量的数据类型,请参考常量声明。

 

2.6 Labels

2.6 标签

 

You can use either an identifier or a non-negative integer number as a label. The Delphi compiler allows numeric labels from 0 to 4294967295 (uint32 range).

你可以使用一个标识符或者一个非负整数作为标签,Delphi编译器允许你使用0~4294967295范围内的数据作为标签(32位范围)。

 

Labels are used in goto statements. For more information about goto statements and labels, see Goto Statements in Declarations and Statements.

标签用于goto语句中。关于goto语句的更多信息,请参考Goto语句。

 

2.7 Character Strings

2.7 常量字符串

 

A character string, also called a string literal or string constant, consists of a quoted string, a control string, or a combination of quoted and control strings. Separators can occur only within quoted strings.

常量字符串(character string)也称为文字串(string literal)或串常量(string const),它由引用串(由一对单引号括起来的文字串)、控制串(控制符构成的串)或这两种串的组合而构成。只有引用串可以包含分隔符。

 

A quoted string is a sequence of characters, from an ANSI or multibyte character set, written on one line and enclosed by apostrophes. A quoted string with nothing between the apostrophes is a null string. Two sequential apostrophes in a quoted string denote a single character, namely an apostrophe.

带引号的字符串是一个字符序列,它的字符来自ANSI或者多字节字符集,写在一行中,并通过单引号括起来。带引号的字符串用单引号之间没有什么表示一个空字符串。在带引号的字符串两个连续的撇号表示一个字符,即一个单引号。

 

The string is represented internally as a Unicode string encoded as UTF-16. Characters in the Basic Multilingual Plane (BMP) take 2 bytes, and characters not in the BMP require 4 bytes.

该字符串在内部表示为一个UTF-16的Unicode字符串编码。基本多文种平面(BMP)内的字符占用2个字节,不属于BMP的字符串需要4个字节。

注:基本多文种平面(Basic Multilingual Plane, BMP),或称第零平面零号平面(Plane 0),是Unicode中的一个编码区段。编码从U+0000至U+FFFF。

 

For example:

例如

 

 'Embarcadero' { Embarcadero }

'You''ll see' { You'll see }

'アプリケーションを Unicode 対応にする'

'''' { ' }

'' { null string }

' ' { a space }

 

 

A control string is a sequence of one or more control characters, each of which consists of the # symbol followed by an unsigned integer constant from 0 to 65,535 (decimal) or from $0 to $FFFF (hexadecimal) in UTF-16 encoding, and denotes the character corresponding to a specified code value. Each integer is represented internally by 2 bytes in the string. This is useful for representing control characters and multibyte characters. The control string:

控制串由一个或多个控制字符(控制符)所组成,每个控制符包含一个#,后跟一个无符号整数从0~65535(10进制)或者整数的范围从0到FFFF(16进制),表示一个相应的字符。每个字符串是由其内部的2个bytes内部构成。这对表示控制字符或多字节字符是非常有用的。下面的控制串:

 

 

#89#111#117

 

 

就相当于引用串:

is equivalent to the quoted string:

'You'

 

 

You can combine quoted strings with control strings to form larger character strings. For example, you could use:

你可以组合引用串和控制串来构成一个更大的串。例如,你能使用:

'Line 1'#13#10'Line 2'

 

 

to put a carriage-return line-feed between 'Line 1' and 'Line 2'. However, you cannot concatenate two quoted strings in this way, since a pair of sequential apostrophes is interpreted as a single character. (To concatenate quoted strings, use the + operator or simply combine them into a single quoted string.)

它在字符串 ”Line 1”  和  ”Line 2” 之间放一个回车(#13)换行(#10)符。但你不能使用这种方式组合两个引用串,因为两个连续的单引号被解释为一个单引号。要组合多个引用串,可以使用“+”运算符,或简单地把它们合并成一个引用串。

 

A character string is compatible with any string type and with the PChar type. Since an AnsiString type may contain multibyte characters, a character string with one character, single or multibyte, is compatible with any character type. When extended syntax is enabled (with compiler directive {$X+}), a nonempty character string of length n is compatible with zero-based arrays and packed arrays of n characters. For more information, see Data Types, Variables, and Constants.

一个字符串,与任何字符串(string)类型以及PChar类型是兼容的;由于一个AnsiString类型可能包含多字节字符,一个字符,单或多字节,一个字符串与任何字符类型是兼容的。当启用扩展语法时({$X+}),一个长度为n的非空字符串,和下标从0开始、包含n个字符的数组以及压缩(packed)数组也是兼容的。关于字符串类型的更多信息,请参考字符串类型。


相关阅读:
Top