Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C-sharp language specification.2004.pdf
Скачиваний:
14
Добавлен:
23.08.2013
Размер:
2.55 Mб
Скачать

C# LANGUAGE SPECIFICATION

1Except for pre-processing directives, skipped source code is not subject to lexical analysis. For example, the

2following is valid despite the unterminated comment in the #else section:

3

#define Debug

// Debugging on

4class PurchaseTransaction

5{

6

void Commit() {

7

#if Debug

8

CheckConsistency();

9

#else

10

/* Do something else

11

#endif

12

}

13

14}

15Note, however, that pre-processing directives are required to be lexically correct even in skipped sections of

16source code.

17Pre-processing directives are not processed when they appear inside multi-line input elements. For example,

18the program:

19class Hello

20{

21

static void Main() {

22

System.Console.WriteLine(@"hello,

23

#if Debug

24

world

25

#else

26

Nebraska

27

#endif

28

");

29}

30}

31results in the output:

32hello,

33#if Debug

34

world

35

#else

36

Nebraska

37#endif

38In peculiar cases, the set of pre-processing directives that is processed might depend on the evaluation of the

39pp-expression. The example:

40#if X

41/*

42#else

43/* */ class Q { }

44#endif

45always produces the same token stream (class Q { }), regardless of whether or not X is defined. If X is

46defined, the only processed directives are #if and #endif, due to the multi-line comment. If X is

47undefined, then three directives (#if, #else, #endif) are part of the directive set. end example]

489.5.5 Diagnostic directives

49The diagnostic directives are used to explicitly generate error and warning messages that are reported in the

50same way as other compile-time errors and warnings.

51pp-diagnostic::

52

whitespaceopt

#

whitespaceopt

error pp-message

53

whitespaceopt

#

whitespaceopt

warning pp-message

80

Chapter 9 Lexical structure

1

pp-message::

2

new-line

3

whitespace input-charactersopt new-line

4[Example: The example

5#warning Code review needed before check-in

6#if Debug && Retail

7#error A build can't be both debug and retail

8#endif

9class Test {…}

10 always produces a warning (“Code review needed before check-in”), and produces a compile-time

11error if the pre-processing identifiers Debug and Retail are both defined. Note that a pp-message can

12contain arbitrary text; specifically, it need not contain well-formed tokens, as shown by the single quote in

13the word can't. end example]

149.5.6 Region control

15The region directives are used to explicitly mark regions of source code.

16pp-region::

17

pp-start-region conditional-sectionopt pp-end-region

18

pp-start-region::

 

 

 

19

whitespaceopt

#

whitespaceopt

region pp-message

20

pp-end-region::

 

 

 

21

whitespaceopt

#

whitespaceopt

endregion pp-message

22No semantic meaning is attached to a region; regions are intended for use by the programmer or by

23automated tools to mark a section of source code. The message specified in a #region or #endregion

24directive likewise has no semantic meaning; it merely serves to identify the region. Matching #region and

25#endregion directives can have different pp-messages.

26The lexical processing of a region:

27#region

28...

29#endregion

30corresponds exactly to the lexical processing of a conditional compilation directive of the form:

31#if true

32...

33#endif

349.5.7 Line directives

35Line directives can be used to alter the line numbers and source file names that are reported by the compiler

36in output such as warnings and errors.

37[Note: Line directives are most commonly used in meta-programming tools that generate C# source code

38from some other text input. end note]

39pp-line::

40

whitespaceopt # whitespaceopt line whitespace line-indicator pp-new-line

41

line-indicator::

42

decimal-digits whitespace file-name

43

decimal-digits

44

identifier-or-keyword

45

file-name::

46

" file-name-characters "

81

 

C# LANGUAGE SPECIFICATION

1

file-name-characters::

2

file-name-character

3

file-name-characters file-name-character

4

file-name-character::

5

Any character except " (U+0022), and new-line-character

6When no #line directives are present, the compiler reports true line numbers and source file names in its

7output. When processing a #line directive that includes a line-indicator that is not identifier-or-keyword,

8the compiler treats the line after the directive as having the given line number (and file name, if specified).

9A #line directive in which the line-indicator is an identifier-or-keyword whose value equals default

10(using equality as specified in §9.4.2) reverses the effect of all preceding #line directives. The compiler

11reports true line information for subsequent lines, precisely as if no #line directives had been processed.

12The purpose of a line-indicator with an identifier-or-keyword whose value does not equal default is

13implementation-defined. An implementation that does not recognize such an identifier-or-keyword in a line-

14indicator shall issue a warning.

15[Note: Note that a file-name differs from a regular string literal in that escape characters are not processed;

16the ‘\’ character simply designates an ordinary back-slash character within a file-name. end note]

179.5.8 Pragma directives

18The #pragma directive is a preprocessing directive used to specify contextual information to a compiler.

19[Note: For example, a compiler might provide #pragma directives that

20Enable or disable particular warning messages when compiling subsequent code.

21Specify which optimizations to apply to subsequent code.

22Specify information to be used by a debugger.

23end note]

24pp-pragma:

25

whitespaceopt # whitespaceopt

pragma pp-pragma-text

26

pp-pragma-text:

 

27

new-line

 

28

whitespace input-charactersopt

new-line

29The input-characters in the pp-pragma-text are interpreted by the compiler in an implementation-defined

30manner. The information supplied in a #pragma directive shall not change program semantics. A #pragma

31directive shall only change compiler behavior which is outside the scope of this language specification. If

32the compiler cannot interpret the input-characters, the compiler can produce a warning; however, it shall not

33produce a compile-time error.

34[Note: pp-pragma-text can contain arbitrary text; specifically, it need not contain well-formed tokens. end

35note]

82

Соседние файлы в предмете Электротехника