lexical_structure.rst 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. .. _lexical_structure:
  2. =================
  3. Lexical Structure
  4. =================
  5. .. index:: single: lexical structure
  6. -----------
  7. Identifiers
  8. -----------
  9. .. index:: single: identifiers
  10. Identifiers start with an alphabetic character or the symbol '_' followed by any number
  11. of alphabetic characters, '_' or digits ([0-9]). Squirrel is a case sensitive language
  12. meaning that the lowercase and uppercase representation of the same alphabetic
  13. character are considered different characters. For instance, "foo", "Foo" and "fOo" are
  14. treated as 3 distinct identifiers.
  15. -----------
  16. Keywords
  17. -----------
  18. .. index:: single: keywords
  19. The following words are reserved and cannot be used as identifiers:
  20. +------------+------------+-----------+------------+------------+-------------+
  21. | base | break | case | catch | class | clone |
  22. +------------+------------+-----------+------------+------------+-------------+
  23. | continue | const | default | delete | else | enum |
  24. +------------+------------+-----------+------------+------------+-------------+
  25. | extends | for | foreach | function | if | in |
  26. +------------+------------+-----------+------------+------------+-------------+
  27. | local | null | resume | return | switch | this |
  28. +------------+------------+-----------+------------+------------+-------------+
  29. | throw | try | typeof | while | yield | constructor |
  30. +------------+------------+-----------+------------+------------+-------------+
  31. | instanceof | true | false | static | __LINE__ | __FILE__ |
  32. +------------+------------+-----------+------------+------------+-------------+
  33. | rawcall | | | | | |
  34. +------------+------------+-----------+------------+------------+-------------+
  35. Keywords are covered in detail later in this document.
  36. -----------
  37. Operators
  38. -----------
  39. .. index:: single: operators
  40. Squirrel recognizes the following operators:
  41. +----------+----------+----------+----------+----------+----------+----------+----------+
  42. | ``!`` | ``!=`` | ``||`` | ``==`` | ``&&`` | ``>=`` | ``<=`` | ``>`` |
  43. +----------+----------+----------+----------+----------+----------+----------+----------+
  44. | ``<=>`` | ``+`` | ``+=`` | ``-`` | ``-=`` | ``/`` | ``/=`` | ``*`` |
  45. +----------+----------+----------+----------+----------+----------+----------+----------+
  46. | ``*=`` | ``%`` | ``%=`` | ``++`` | ``--`` | ``<-`` | ``=`` | ``&`` |
  47. +----------+----------+----------+----------+----------+----------+----------+----------+
  48. | ``^`` | ``|`` | ``~`` | ``>>`` | ``<<`` | ``>>>`` | | |
  49. +----------+----------+----------+----------+----------+----------+----------+----------+
  50. ------------
  51. Other tokens
  52. ------------
  53. .. index::
  54. single: delimiters
  55. single: other tokens
  56. Other significant tokens are:
  57. +----------+----------+----------+----------+----------+----------+
  58. | ``{`` | ``}`` | ``[`` | ``]`` | ``.`` | ``:`` |
  59. +----------+----------+----------+----------+----------+----------+
  60. | ``::`` | ``'`` | ``;`` | ``"`` | ``@"`` | |
  61. +----------+----------+----------+----------+----------+----------+
  62. -----------
  63. Literals
  64. -----------
  65. .. index::
  66. single: literals
  67. single: string literals
  68. single: numeric literals
  69. Squirrel accepts integer numbers, floating point numbers and string literals.
  70. +-------------------------------+------------------------------------------+
  71. | ``34`` | Integer number(base 10) |
  72. +-------------------------------+------------------------------------------+
  73. | ``0xFF00A120`` | Integer number(base 16) |
  74. +-------------------------------+------------------------------------------+
  75. | ``0753`` | Integer number(base 8) |
  76. +-------------------------------+------------------------------------------+
  77. | ``'a'`` | Integer number |
  78. +-------------------------------+------------------------------------------+
  79. | ``1.52`` | Floating point number |
  80. +-------------------------------+------------------------------------------+
  81. | ``1.e2`` | Floating point number |
  82. +-------------------------------+------------------------------------------+
  83. | ``1.e-2`` | Floating point number |
  84. +-------------------------------+------------------------------------------+
  85. | ``"I'm a string"`` | String |
  86. +-------------------------------+------------------------------------------+
  87. | ``@"I'm a verbatim string"`` | String |
  88. +-------------------------------+------------------------------------------+
  89. | ``@" I'm a`` | |
  90. | ``multiline verbatim string`` | |
  91. | ``"`` | String |
  92. +-------------------------------+------------------------------------------+
  93. Pesudo BNF
  94. .. productionlist::
  95. IntegerLiteral : [1-9][0-9]* | '0x' [0-9A-Fa-f]+ | ''' [.]+ ''' | 0[0-7]+
  96. FloatLiteral : [0-9]+ '.' [0-9]+
  97. FloatLiteral : [0-9]+ '.' 'e'|'E' '+'|'-' [0-9]+
  98. StringLiteral: '"'[.]* '"'
  99. VerbatimStringLiteral: '@''"'[.]* '"'
  100. -----------
  101. Comments
  102. -----------
  103. .. index:: single: comments
  104. A comment is text that the compiler ignores but that is useful for programmers.
  105. Comments are normally used to embed annotations in the code. The compiler
  106. treats them as white space.
  107. A comment can be ``/*`` (slash, asterisk) characters, followed by any
  108. sequence of characters (including new lines),
  109. followed by the ``*/`` characters. This syntax is the same as ANSI C.::
  110. /*
  111. this is
  112. a multiline comment.
  113. this lines will be ignored by the compiler
  114. */
  115. A comment can also be ``//`` (two slashes) characters, followed by any sequence of
  116. characters. A new line not immediately preceded by a backslash terminates this form of
  117. comment. It is commonly called a *"single-line comment."*::
  118. //this is a single line comment. this line will be ignored by the compiler
  119. The character ``#`` is an alternative syntax for single line comment.::
  120. # this is also a single line comment.
  121. This to facilitate the use of squirrel in UNIX-style shell scripts.