lexical_structure.rst 6.3 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 a alphabetic character or '_' followed by any number of alphabetic
  11. characters, '_' or digits ([0-9]). Squirrel is a case sensitive language, this means that the
  12. lowercase and uppercase representation of the same alphabetic character are considered
  13. different characters. For instance "foo", "Foo" and "fOo" will be treated as 3 distinct
  14. identifiers.
  15. -----------
  16. Keywords
  17. -----------
  18. .. index:: single: keywords
  19. The following words are reserved words by the language 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. Keywords are covered in detail later in this document.
  34. -----------
  35. Operators
  36. -----------
  37. .. index:: single: operators
  38. Squirrel recognizes the following operators:
  39. +----------+----------+----------+----------+----------+----------+----------+----------+
  40. | ``!`` | ``!=`` | ``||`` | ``==`` | ``&&`` | ``>=`` | ``<=`` | ``>`` |
  41. +----------+----------+----------+----------+----------+----------+----------+----------+
  42. | ``<=>`` | ``+`` | ``+=`` | ``-`` | ``-=`` | ``/`` | ``/=`` | ``*`` |
  43. +----------+----------+----------+----------+----------+----------+----------+----------+
  44. | ``*=`` | ``%`` | ``%=`` | ``++`` | ``--`` | ``<-`` | ``=`` | ``&`` |
  45. +----------+----------+----------+----------+----------+----------+----------+----------+
  46. | ``^`` | ``|`` | ``~`` | ``>>`` | ``<<`` | ``>>>`` | | |
  47. +----------+----------+----------+----------+----------+----------+----------+----------+
  48. ------------
  49. Other tokens
  50. ------------
  51. .. index::
  52. single: delimiters
  53. single: other tokens
  54. Other used tokens are:
  55. +----------+----------+----------+----------+----------+----------+
  56. | ``{`` | ``}`` | ``[`` | ``]`` | ``.`` | ``:`` |
  57. +----------+----------+----------+----------+----------+----------+
  58. | ``::`` | ``'`` | ``;`` | ``"`` | ``@"`` | |
  59. +----------+----------+----------+----------+----------+----------+
  60. -----------
  61. Literals
  62. -----------
  63. .. index::
  64. single: literals
  65. single: string literals
  66. single: numeric literals
  67. Squirrel accepts integer numbers, floating point numbers and strings literals.
  68. +-------------------------------+------------------------------------------+
  69. | ``34`` | Integer number(base 10) |
  70. +-------------------------------+------------------------------------------+
  71. | ``0xFF00A120`` | Integer number(base 16) |
  72. +-------------------------------+------------------------------------------+
  73. | ``0753`` | Integer number(base 8) |
  74. +-------------------------------+------------------------------------------+
  75. | ``'a'`` | Integer number |
  76. +-------------------------------+------------------------------------------+
  77. | ``1.52`` | Floating point number |
  78. +-------------------------------+------------------------------------------+
  79. | ``1.e2`` | Floating point number |
  80. +-------------------------------+------------------------------------------+
  81. | ``1.e-2`` | Floating point number |
  82. +-------------------------------+------------------------------------------+
  83. | ``"I'm a string"`` | String |
  84. +-------------------------------+------------------------------------------+
  85. | ``@"I'm a verbatim string"`` | String |
  86. +-------------------------------+------------------------------------------+
  87. | ``@" I'm a`` | |
  88. | ``multiline verbatim string`` | |
  89. | ``"`` | String |
  90. +-------------------------------+------------------------------------------+
  91. Pesudo BNF
  92. .. productionlist::
  93. IntegerLiteral : [1-9][0-9]* | '0x' [0-9A-Fa-f]+ | ''' [.]+ ''' | 0[0-7]+
  94. FloatLiteral : [0-9]+ '.' [0-9]+
  95. FloatLiteral : [0-9]+ '.' 'e'|'E' '+'|'-' [0-9]+
  96. StringLiteral: '"'[.]* '"'
  97. VerbatimStringLiteral: '@''"'[.]* '"'
  98. -----------
  99. Comments
  100. -----------
  101. .. index:: single: comments
  102. A comment is text that the compiler ignores but that is useful for programmers.
  103. Comments are normally used to embed annotations in the code. The compiler
  104. treats them as white space.
  105. The ``/*`` (slash, asterisk) characters, followed by any
  106. sequence of characters (including new lines),
  107. followed by the ``*/`` characters. This syntax is the same as ANSI C.::
  108. /*
  109. this is
  110. a multiline comment.
  111. this lines will be ignored by the compiler
  112. */
  113. The ``//`` (two slashes) characters, followed by any sequence of characters.
  114. A new line not immediately preceded by a backslash terminates this form of comment.
  115. It is commonly called a *"single-line comment."*::
  116. //this is a single line comment. this line will be ignored by the compiler
  117. The character ``#`` is an alternative syntax for single line comment.::
  118. # this is also a single line comment.
  119. This to facilitate the use of squirrel in UNIX-style shell scripts.