af-mobile-client-vue3 1.3.36 → 1.3.37

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (281) hide show
  1. package/.cursorrules +60 -60
  2. package/.editorconfig +9 -9
  3. package/.env +10 -10
  4. package/.env.development +1 -1
  5. package/.env.production +1 -1
  6. package/.node-version +1 -1
  7. package/.vscode/extensions.json +12 -12
  8. package/.vscode/settings.json +68 -68
  9. package/CLAUDE.md +218 -218
  10. package/README.md +182 -182
  11. package/af-example-mobile-vue-web.iml +9 -9
  12. package/build/vite/index.ts +98 -98
  13. package/build/vite/optimize.ts +34 -34
  14. package/build/vite/vconsole.ts +47 -47
  15. package/commitlint.config.ts +32 -32
  16. package/compress.js +36 -36
  17. package/eslint.config.ts +31 -31
  18. package/index.html +23 -23
  19. package/mock/data.ts +20 -20
  20. package/mock/index.ts +7 -7
  21. package/mock/modules/prose.mock.ts +13 -13
  22. package/mock/modules/user.mock.ts +95 -95
  23. package/mock/util.ts +19 -19
  24. package/netlify.toml +12 -12
  25. package/package.json +114 -114
  26. package/postcss.config.ts +27 -27
  27. package/public/favicon.svg +4 -4
  28. package/public/safari-pinned-tab.svg +4 -4
  29. package/scripts/verifyCommit.js +19 -19
  30. package/src/App.vue +79 -79
  31. package/src/api/auth/index.ts +77 -77
  32. package/src/api/auth/types.ts +200 -200
  33. package/src/api/mock/index.ts +30 -30
  34. package/src/api/user/index.ts +40 -40
  35. package/src/assets/img/user/login/background-shadow-1.svg +20 -20
  36. package/src/assets/img/user/login/logo-background.svg +20 -20
  37. package/src/bootstrap.ts +26 -26
  38. package/src/components/core/BeautifulLoading/index.vue +52 -52
  39. package/src/components/core/ImageUploader/index.vue +251 -251
  40. package/src/components/core/NavBar/index.vue +53 -53
  41. package/src/components/core/Tabbar/index.vue +32 -32
  42. package/src/components/core/Uploader/index.vue +124 -124
  43. package/src/components/core/XGridDropOption/index.vue +154 -154
  44. package/src/components/core/XMultiSelect/index.vue +183 -183
  45. package/src/components/core/XSelect/index.vue +149 -149
  46. package/src/components/data/CardContainer/CardContainer.vue +118 -118
  47. package/src/components/data/CardContainer/CardHeader.vue +99 -99
  48. package/src/components/data/InfoDisplay/index.vue +132 -132
  49. package/src/components/data/UserDetail/api.ts +24 -24
  50. package/src/components/data/UserDetail/index.vue +620 -620
  51. package/src/components/data/UserDetail/recordEntries.ts +159 -159
  52. package/src/components/data/UserDetail/types.ts +26 -26
  53. package/src/components/data/XBadge/index.vue +82 -82
  54. package/src/components/data/XCellDetail/index.vue +105 -105
  55. package/src/components/data/XCellList/XCellList.md +432 -432
  56. package/src/components/data/XCellList/index.vue +1436 -1436
  57. package/src/components/data/XCellListFilter/QrScanner/index.vue +207 -207
  58. package/src/components/data/XCellListFilter/QrScanner/startScanAnimation.ts +53 -53
  59. package/src/components/data/XCellListFilter/VpnRecognition/index.vue +119 -119
  60. package/src/components/data/XCellListFilter/index.vue +705 -705
  61. package/src/components/data/XForm/index.vue +659 -659
  62. package/src/components/data/XFormGroup/doc/DeviceForm.vue +122 -122
  63. package/src/components/data/XFormGroup/doc/FormGroupDemo.vue +56 -56
  64. package/src/components/data/XFormGroup/doc/README.md +286 -286
  65. package/src/components/data/XFormGroup/doc/UserForm.vue +102 -102
  66. package/src/components/data/XFormGroup/index.vue +240 -240
  67. package/src/components/data/XFormItem/index.vue +1311 -1311
  68. package/src/components/data/XOlMap/README.md +227 -227
  69. package/src/components/data/XOlMap/XLocationPicker/index.vue +226 -226
  70. package/src/components/data/XOlMap/index.vue +1490 -1490
  71. package/src/components/data/XOlMap/types.ts +149 -149
  72. package/src/components/data/XOlMap/utils/wgs84ToGcj02.ts +171 -171
  73. package/src/components/data/XReportForm/DateTimeSecondsPicker.vue +208 -208
  74. package/src/components/data/XReportForm/XReportFormJsonRender.vue +220 -220
  75. package/src/components/data/XReportForm/index.vue +1393 -1393
  76. package/src/components/data/XReportGrid/XAddReport/XAddReport.vue +198 -198
  77. package/src/components/data/XReportGrid/XAddReport/index.js +3 -3
  78. package/src/components/data/XReportGrid/XAddReport/index.md +53 -53
  79. package/src/components/data/XReportGrid/XAddReport/index.ts +10 -10
  80. package/src/components/data/XReportGrid/XReport.vue +960 -960
  81. package/src/components/data/XReportGrid/XReportDemo.vue +33 -33
  82. package/src/components/data/XReportGrid/XReportDesign.vue +597 -597
  83. package/src/components/data/XReportGrid/XReportDrawer/XReportDrawer.vue +148 -148
  84. package/src/components/data/XReportGrid/XReportDrawer/index.js +3 -3
  85. package/src/components/data/XReportGrid/XReportDrawer/index.ts +10 -10
  86. package/src/components/data/XReportGrid/XReportJsonRender.vue +399 -399
  87. package/src/components/data/XReportGrid/XReportTrGroup.vue +592 -592
  88. package/src/components/data/XReportGrid/index.md +46 -46
  89. package/src/components/data/XReportGrid/print.js +184 -184
  90. package/src/components/data/XSignature/index.vue +284 -284
  91. package/src/components/data/XTag/index.vue +10 -10
  92. package/src/components/layout/NormalDataLayout/index.vue +69 -69
  93. package/src/components/layout/TabBarLayout/index.vue +40 -40
  94. package/src/composables/dark.ts +5 -5
  95. package/src/config/routes.ts +9 -9
  96. package/src/constants/index.ts +2 -2
  97. package/src/enums/requestEnum.ts +25 -25
  98. package/src/expression/ExpressionRunner.ts +28 -28
  99. package/src/expression/TestExpression.ts +510 -510
  100. package/src/expression/core/Delegate.ts +116 -116
  101. package/src/expression/core/Expression.ts +1359 -1359
  102. package/src/expression/core/Program.ts +985 -985
  103. package/src/expression/core/Token.ts +29 -29
  104. package/src/expression/enums/ExpressionType.ts +81 -81
  105. package/src/expression/enums/TokenType.ts +11 -11
  106. package/src/expression/exception/BreakWayException.ts +2 -2
  107. package/src/expression/exception/ContinueWayException.ts +2 -2
  108. package/src/expression/exception/ExpressionException.ts +29 -29
  109. package/src/expression/exception/ReturnWayException.ts +14 -14
  110. package/src/expression/exception/ServiceException.ts +22 -22
  111. package/src/expression/instances/JSONArray.ts +52 -52
  112. package/src/expression/instances/JSONObject.ts +118 -118
  113. package/src/expression/instances/LogicConsole.ts +31 -31
  114. package/src/font-style/font.css +4 -4
  115. package/src/hooks/useBoolean.ts +26 -26
  116. package/src/hooks/useCommon.ts +9 -9
  117. package/src/hooks/useLoading.ts +16 -16
  118. package/src/hooks/useLogin.ts +97 -97
  119. package/src/icons/svg/check-in.svg +32 -32
  120. package/src/icons/svg/dark.svg +4 -4
  121. package/src/icons/svg/github.svg +4 -4
  122. package/src/icons/svg/light.svg +4 -4
  123. package/src/icons/svg/link.svg +4 -4
  124. package/src/icons/svgo.yml +22 -22
  125. package/src/layout/GridView/index.vue +16 -16
  126. package/src/layout/PageLayout.vue +9 -9
  127. package/src/layout/SingleLayout.vue +9 -9
  128. package/src/locales/en-US.json +128 -128
  129. package/src/locales/zh-CN.json +128 -128
  130. package/src/logic/LogicRunner.ts +67 -67
  131. package/src/logic/TestLogic.ts +13 -13
  132. package/src/logic/plugins/common/DateTools.ts +35 -35
  133. package/src/logic/plugins/common/VueTools.ts +30 -30
  134. package/src/logic/plugins/index.ts +7 -7
  135. package/src/main.ts +44 -44
  136. package/src/plugins/AppData.ts +38 -38
  137. package/src/plugins/GetLoginInfoService.ts +10 -10
  138. package/src/plugins/collectIcons.ts +10 -10
  139. package/src/plugins/index.ts +11 -11
  140. package/src/router/README.md +8 -8
  141. package/src/router/external-routes.ts +60 -60
  142. package/src/router/guards.ts +131 -131
  143. package/src/router/index.ts +35 -35
  144. package/src/router/invoiceRoutes.ts +33 -33
  145. package/src/router/routes.ts +421 -421
  146. package/src/services/api/Login.ts +6 -6
  147. package/src/services/api/common.ts +109 -109
  148. package/src/services/api/index.ts +7 -7
  149. package/src/services/api/manage.ts +8 -8
  150. package/src/services/api/search.ts +16 -16
  151. package/src/services/api/user.ts +17 -17
  152. package/src/services/restTools.ts +56 -56
  153. package/src/services/v3Api.ts +147 -147
  154. package/src/stores/index.ts +13 -13
  155. package/src/stores/modules/counter.ts +19 -19
  156. package/src/stores/modules/homeApp.ts +55 -55
  157. package/src/stores/modules/routeCache.ts +22 -22
  158. package/src/stores/modules/setting.ts +87 -87
  159. package/src/stores/modules/user.ts +326 -326
  160. package/src/stores/mutation-type.ts +12 -12
  161. package/src/styles/app.less +36 -36
  162. package/src/styles/login.less +109 -109
  163. package/src/styles/var.less +25 -25
  164. package/src/types/auth.ts +89 -89
  165. package/src/types/env.d.ts +16 -16
  166. package/src/types/platform.ts +194 -194
  167. package/src/types/settings.ts +1 -1
  168. package/src/types/vue-router.d.ts +13 -13
  169. package/src/utils/Storage.ts +124 -124
  170. package/src/utils/authority-utils.ts +84 -84
  171. package/src/utils/common.ts +41 -41
  172. package/src/utils/crypto.ts +39 -39
  173. package/src/utils/dataUtil.ts +42 -42
  174. package/src/utils/dictUtil.ts +52 -52
  175. package/src/utils/http/index.ts +201 -201
  176. package/src/utils/i18n.ts +72 -72
  177. package/src/utils/indexedDB.ts +195 -195
  178. package/src/utils/inline-px-to-vw.ts +28 -28
  179. package/src/utils/mobileUtil.ts +33 -33
  180. package/src/utils/platform-auth.ts +135 -135
  181. package/src/utils/progress.ts +19 -19
  182. package/src/utils/queryFormDefaultRangePicker.ts +57 -57
  183. package/src/utils/routerUtil.ts +271 -271
  184. package/src/utils/runEvalFunction.ts +13 -13
  185. package/src/utils/secureStorage.ts +70 -70
  186. package/src/utils/set-page-title.ts +5 -5
  187. package/src/utils/validate.ts +6 -6
  188. package/src/views/chat/index.vue +153 -153
  189. package/src/views/common/Forbidden.vue +97 -97
  190. package/src/views/common/LoadError.vue +63 -63
  191. package/src/views/common/NotFound.vue +67 -67
  192. package/src/views/component/EvaluateRecordView/index.vue +40 -40
  193. package/src/views/component/IconifyView/index.vue +504 -504
  194. package/src/views/component/UserDetailView/UserDetailPage.vue +77 -77
  195. package/src/views/component/UserDetailView/index.vue +234 -234
  196. package/src/views/component/XCellDetailView/index.vue +217 -217
  197. package/src/views/component/XCellListView/index.vue +108 -108
  198. package/src/views/component/XFormAppraiseView/index.vue +174 -174
  199. package/src/views/component/XFormGroupView/index.vue +78 -78
  200. package/src/views/component/XFormView/index.vue +27 -27
  201. package/src/views/component/XOlMapView/index.vue +434 -434
  202. package/src/views/component/XOlMapView/testData.ts +64 -64
  203. package/src/views/component/XReportFormIframeView/index.vue +47 -47
  204. package/src/views/component/XReportFormView/index.vue +13 -13
  205. package/src/views/component/XReportGridView/index.vue +17 -17
  206. package/src/views/component/XRequestView/index.vue +234 -234
  207. package/src/views/component/XSignatureView/index.vue +50 -50
  208. package/src/views/component/index.vue +181 -181
  209. package/src/views/component/menu.vue +117 -117
  210. package/src/views/component/notice.vue +46 -46
  211. package/src/views/component/topNav.vue +36 -36
  212. package/src/views/external/index.vue +152 -152
  213. package/src/views/invoiceShow/index.vue +61 -61
  214. package/src/views/loading/AuthLoading.vue +345 -345
  215. package/src/views/user/login/ForgetPasswordForm.vue +94 -94
  216. package/src/views/user/login/LoginForm.vue +350 -350
  217. package/src/views/user/login/LoginTitle.vue +76 -76
  218. package/src/views/user/login/LoginWave.vue +109 -109
  219. package/src/views/user/login/index.vue +22 -22
  220. package/src/views/user/my/comm/ModifyPassword.vue +346 -346
  221. package/src/views/user/my/index.vue +507 -507
  222. package/src/views/user/register/index.vue +952 -952
  223. package/src/views/userRecords/AbnormalAlarmRecords.vue +21 -21
  224. package/src/views/userRecords/CardReplacementRecords.vue +21 -21
  225. package/src/views/userRecords/ChangeRecords.vue +19 -19
  226. package/src/views/userRecords/CommandViewRecords.vue +20 -20
  227. package/src/views/userRecords/GasCompensationRecords.vue +20 -20
  228. package/src/views/userRecords/InstrumentCollectionRecords.vue +21 -21
  229. package/src/views/userRecords/MeterRecords.vue +20 -20
  230. package/src/views/userRecords/OperateRecords.vue +51 -51
  231. package/src/views/userRecords/OtherChargeRecords.vue +19 -19
  232. package/src/views/userRecords/PaymentRecords.vue +28 -28
  233. package/src/views/userRecords/PriceAdjustmentRecords.vue +19 -19
  234. package/src/views/userRecords/ReplacementRecords.vue +19 -19
  235. package/src/views/userRecords/SafetyRecords.vue +19 -19
  236. package/src/views/userRecords/TransactionRecords.vue +21 -21
  237. package/src/views/userRecords/TransferRecords.vue +19 -19
  238. package/src/views/userRecords/operateRecordDetail/index.vue +316 -316
  239. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/AddUserDetail.vue +124 -124
  240. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/AdvanceDeliveryDetail.vue +88 -88
  241. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/AutoAccountsCancelDetail.vue +205 -205
  242. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/AutoAccountsDetail.vue +192 -192
  243. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/BankDkDetail.vue +192 -192
  244. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/BankPayDetail.vue +192 -192
  245. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/BlacklistDetail.vue +153 -153
  246. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/CancellationDetail.vue +101 -101
  247. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/CardMeterCenterCancelDetail.vue +127 -127
  248. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/CardMeterCenterDetail.vue +153 -153
  249. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/CardOverUserDetail.vue +153 -153
  250. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/ChangeMeterCancelDetail.vue +166 -166
  251. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/ChangeMeterDetail.vue +205 -205
  252. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/DisableManageDetail.vue +127 -127
  253. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/EnableManageDetail.vue +114 -114
  254. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/FaZheChangeDetail.vue +124 -124
  255. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/FeeDeductionDetail.vue +153 -153
  256. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/GasPriceChangeDetail.vue +126 -126
  257. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/InputtorChangeDetail.vue +126 -126
  258. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/IotMeterCenterCancelDetail.vue +114 -114
  259. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/IotMeterCenterDetail.vue +127 -127
  260. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/IotOpenDetail.vue +88 -88
  261. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/MachineCardDetail.vue +101 -101
  262. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/MachineMeterCenterCancelDetail.vue +218 -218
  263. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/MachineMeterCenterDetail.vue +153 -153
  264. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/OffGasAddGasDetail.vue +140 -140
  265. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/OtherChargeCancelDetail.vue +127 -127
  266. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/OtherChargeDetail.vue +114 -114
  267. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/OverUserChangeDetail.vue +127 -127
  268. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/ReBillDetail.vue +127 -127
  269. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/RefundDetail.vue +114 -114
  270. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/ReplaceCardManageCancelDetail.vue +127 -127
  271. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/ReplaceCardManageDetail.vue +114 -114
  272. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/SaleCardGasDetail.vue +140 -140
  273. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/TransferManageCancelDetail.vue +152 -152
  274. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/TransferManageDetail.vue +178 -178
  275. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/UserChangeDetail.vue +123 -123
  276. package/src/views/userRecords/operateRecordDetail/operateRecordDetails/WechatPayDetail.vue +192 -192
  277. package/src/views/userRecords/types.ts +66 -66
  278. package/tsconfig.json +39 -39
  279. package/uno.config.ts +82 -82
  280. package/vite.config.ts +114 -114
  281. package/.claude/settings.local.json +0 -14
@@ -1,985 +1,985 @@
1
- import ExpressionType from '../enums/ExpressionType'
2
- import TokenType from '../enums/TokenType'
3
- import JSONArray from '../instances/JSONArray'
4
- import Delegate from './Delegate'
5
- import Expression from './Expression'
6
- import Token from './Token'
7
-
8
- /**
9
- * 表达式入口
10
- */
11
- export default class Program {
12
- // 源程序
13
- public readonly SOURCE: string
14
- public readonly SOURCE_LENGTH: number
15
- /**
16
- * Token队列,用于回退
17
- */
18
- private readonly tokens: Token[] = []
19
- /**
20
- * 字符串处理环节堆栈,$进入字符串处理环节,“{}”中间部分脱离字符串处理环节
21
- */
22
- private readonly inStrings: boolean[] = []
23
-
24
- /**
25
- * 当前获取到的字符位置
26
- */
27
- private pos: number = 0
28
-
29
- /**
30
- * 当前是否在字符串处理环节
31
- */
32
- private inString: boolean = false
33
-
34
- constructor(source: string) {
35
- this.SOURCE = source
36
- this.SOURCE_LENGTH = this.SOURCE.length
37
- }
38
-
39
- /**
40
- * 调用解析过程
41
- */
42
- public parse(): Delegate {
43
- // 执行完以下方法后就已经诞生了一个完整的执行流程,也就是被翻译成了一套完整的指令集。
44
- return this.createDelegate(this.expression())
45
- }
46
-
47
- public parseExpression(expression: Expression): Delegate {
48
- // 执行完以下方法后就已经诞生了一个完整的执行流程,也就是被翻译成了一套完整的指令集。
49
- return this.createDelegate(expression)
50
- }
51
-
52
- /**
53
- * 编译,产生可执行单元Delegate
54
- */
55
- public createDelegate(expression: Expression): Delegate {
56
- return new Delegate(expression, this.SOURCE)
57
- }
58
-
59
- /**
60
- * 获取完整表达式
61
- */
62
- public expression(): Expression {
63
- const expression = this.CommaExp()
64
- if (this.pos < this.SOURCE_LENGTH) {
65
- throw new Error(this.GetExceptionMessage('需要逗号结尾!'))
66
- }
67
- return expression
68
- }
69
-
70
- /**
71
- * 逗号表达式=赋值表达式(,赋值表达式)*
72
- */
73
- private CommaExp(): Expression {
74
- const exps: Expression[] = []
75
-
76
- const first: Expression = this.AssignExp()
77
- exps.push(first)
78
-
79
- let t: Token = this.GetToken()
80
-
81
- // 没有结束
82
- while (t.getType() === TokenType.Oper && (t.getValue() === ',' || t.getValue() === ';' || t.getValue() === '\n')) {
83
- const r: Expression = this.AssignExp()
84
- exps.push(r)
85
- t = this.GetToken()
86
- }
87
-
88
- this.tokens.push(t)
89
-
90
- return Expression.Comma(exps, this.pos)
91
- }
92
-
93
- /**
94
- * 赋值表达式=对象属性=一般表达式|一般表达式
95
- */
96
- private AssignExp(): Expression {
97
- let t: Token = this.GetToken()
98
-
99
- // 把第一个Token的位置记录下来,方便后面回退
100
- const firstPos: number = t.getStartPos()
101
-
102
- if (t.getType() !== TokenType.Identy) {
103
- this.pos = firstPos
104
- this.tokens.length = 0 // Clear the tokens array
105
- this.inString = false
106
- return this.Exp()
107
- }
108
-
109
- let objExp: Expression = Expression.Identity(t.getValue(), this.pos)
110
- objExp = this.ObjectPath(objExp)
111
-
112
- // 只能给对象属性或者变量赋值
113
- if (objExp.type !== ExpressionType.Property && objExp.type !== ExpressionType.Identity) {
114
- this.pos = firstPos
115
- this.tokens.length = 0
116
- this.inString = false
117
- return this.Exp()
118
- }
119
-
120
- t = this.GetToken()
121
- if (t.getType() !== TokenType.Oper || t.getValue() !== '=') {
122
- this.pos = firstPos
123
- this.tokens.length = 0
124
- this.inString = false
125
- return this.Exp()
126
- }
127
-
128
- const exp: Expression = this.Exp()
129
-
130
- // 如果是属性赋值
131
- if (objExp.type === ExpressionType.Property) {
132
- // 从属性Expression中获取对象及属性名
133
- const name: string = <string>objExp.value
134
- objExp = objExp.children[0] as Expression
135
- return Expression.Assign(objExp, exp, name, this.pos)
136
- }
137
- else {
138
- // Identity 变量赋值
139
- return Expression.Assign(null, exp, <string>objExp.value, this.pos)
140
- }
141
- }
142
-
143
- private Exp(): Expression {
144
- const v: Expression = this.Logic()
145
-
146
- // 是':',表示条件,否则直接返回单结果
147
- let t: Token = this.GetToken()
148
- if (t.getType() === TokenType.Oper && t.getValue() === ':') {
149
- // 第一项转换
150
- const result: Expression = this.Logic()
151
-
152
- // 下一个是",",继续读取下一个条件结果串,由于是右结合,只能采用递归
153
- t = this.GetToken()
154
- if (t.getType() === TokenType.Oper && t.getValue() === ',') {
155
- // 第二项转换
156
- const sExp: Expression = this.Exp()
157
- // 返回
158
- return Expression.Condition(v, result, sExp, this.SOURCE, this.pos)
159
- }
160
- else {
161
- throw new Error(this.GetExceptionMessage('必须有默认值!'))
162
- }
163
- }
164
- else {
165
- this.tokens.push(t) // Put the token back
166
- return v
167
- }
168
- }
169
-
170
- private Logic(): Expression {
171
- let t: Token = this.GetToken()
172
-
173
- // !表达式
174
- const hasNot: boolean = t.getType() === TokenType.Oper && t.getValue() === '!'
175
- if (!hasNot) {
176
- this.tokens.push(t) // Put the token back if it's not '!'
177
- }
178
-
179
- let v: Expression = this.Compare()
180
- if (hasNot) {
181
- v = Expression.Not(v, this.SOURCE, this.pos)
182
- }
183
-
184
- t = this.GetToken()
185
- while (t.getType() === TokenType.Oper && (t.getValue() === '&&' || t.getValue() === '||')) {
186
- // 第二项转换
187
- const exp: Expression = this.Logic()
188
-
189
- // 执行
190
- if (t.getValue() === '&&') {
191
- v = Expression.And(v, exp, this.SOURCE, this.pos)
192
- }
193
- else {
194
- v = Expression.Or(v, exp, this.SOURCE, this.pos)
195
- }
196
-
197
- t = this.GetToken()
198
- }
199
-
200
- this.tokens.push(t) // Put the token back
201
- return v
202
- }
203
-
204
- private Compare(): Expression {
205
- const left: Expression = this.Math()
206
- const t: Token = this.GetToken()
207
-
208
- if (t.getType() === TokenType.Oper && (
209
- t.getValue() === '>' || t.getValue() === '>='
210
- || t.getValue() === '<' || t.getValue() === '<=' || t.getValue() === '=>')) {
211
- if (t.getValue() === '=>') {
212
- this.tokens.push(t) // Push back the token for lambda handling
213
- return this.Lambda()
214
- }
215
-
216
- const rExp: Expression = this.Math()
217
-
218
- switch (t.getValue()) {
219
- case '>':
220
- return Expression.GreaterThan(left, rExp, this.SOURCE, this.pos)
221
- case '>=':
222
- return Expression.GreaterThanOrEqual(left, rExp, this.SOURCE, this.pos)
223
- case '<':
224
- return Expression.LessThan(left, rExp, this.SOURCE, this.pos)
225
- case '<=':
226
- return Expression.LessThanOrEqual(left, rExp, this.SOURCE, this.pos)
227
- }
228
- }
229
- else if (t.getType() === TokenType.Oper && (
230
- t.getValue() === '==' || t.getValue() === '!=')) {
231
- const rExp: Expression = this.Math()
232
-
233
- // 相等比较
234
- if (t.getValue() === '==') {
235
- return Expression.Equal(left, rExp, this.SOURCE, this.pos)
236
- }
237
- else {
238
- return Expression.NotEqual(left, rExp, this.SOURCE, this.pos)
239
- }
240
- }
241
-
242
- // 返回当前的表达式结果
243
- this.tokens.push(t) // Push the token back
244
- return left
245
- }
246
-
247
- private Math(): Expression {
248
- let v: Expression = this.Mul()
249
- let t: Token = this.GetToken()
250
-
251
- while (t.getType() === TokenType.Oper && (t.getValue() === '+' || t.getValue() === '-')) {
252
- // 转换操作数2为数字
253
- const r: Expression = this.Mul()
254
-
255
- // 开始运算
256
- if (t.getValue() === '+') {
257
- v = Expression.Add(v, r, this.SOURCE, this.pos)
258
- }
259
- else {
260
- v = Expression.Subtract(v, r, this.SOURCE, this.pos)
261
- }
262
-
263
- t = this.GetToken()
264
- }
265
-
266
- this.tokens.push(t) // Push the token back
267
- return v
268
- }
269
-
270
- private Lambda(): Expression {
271
- let t: Token = this.GetToken()
272
- if (t.getValue() !== '=>' || t.getValue() == null) {
273
- throw new Error(this.GetExceptionMessage('lambda必须以=>开始'))
274
- }
275
-
276
- t = this.GetToken()
277
- if (t.getValue() !== '{' || t.getValue() == null) {
278
- throw new Error(this.GetExceptionMessage('lambda必须以{开始'))
279
- }
280
-
281
- // 取lambda中的表达式
282
- const exp: Expression = this.CommaExp()
283
-
284
- t = this.GetToken()
285
- if (t.getValue() !== '}' || t.getValue() == null) {
286
- throw new Error(this.GetExceptionMessage('lambda必须以}结束'))
287
- }
288
-
289
- return Expression.Lambda(exp, this.pos)
290
- }
291
-
292
- private Mul(): Expression {
293
- let v: Expression = this.UnarySub()
294
- let t: Token = this.GetToken()
295
-
296
- while (t.getType() === TokenType.Oper && (t.getValue() === '*' || t.getValue() === '/' || t.getValue() === '%')) {
297
- // Convert the second operand to a number
298
- const r: Expression = this.UnarySub()
299
-
300
- // Perform the operation
301
- if (t.getValue() === '*') {
302
- v = Expression.Multiply(v, r, this.SOURCE, this.pos)
303
- }
304
- else if (t.getValue() === '/') {
305
- v = Expression.Divide(v, r, this.SOURCE, this.pos)
306
- }
307
- else {
308
- v = Expression.Modulo(v, r, this.SOURCE, this.pos)
309
- }
310
-
311
- t = this.GetToken()
312
- }
313
-
314
- this.tokens.push(t)
315
- return v
316
- }
317
-
318
- private UnarySub(): Expression {
319
- const t: Token = this.GetToken()
320
- if (t.getType() === TokenType.Oper && t.getValue() === '-') {
321
- const r: Expression = this.Item()
322
- return Expression.Subtract(Expression.Constant(0, this.pos), r, this.SOURCE, this.pos)
323
- }
324
- this.tokens.push(t) // Push the token back into the queue
325
- return this.Item()
326
- }
327
-
328
- private Item(): Expression {
329
- const objName: string = ''
330
- // 获取对象表达式
331
- let objExp: Expression = this.ItemHead(objName)
332
- // 获取对象路径表达式
333
- objExp = this.ObjectPath(objExp)
334
- return objExp
335
- }
336
-
337
- private ItemHead(name: string): Expression {
338
- let t: Token = this.GetToken()
339
-
340
- if (t.getType() === TokenType.Oper && t.getValue() === '(') {
341
- const result: Expression = this.CommaExp()
342
- t = this.GetToken()
343
- if (t.getType() !== TokenType.Oper || t.getValue() !== ')') {
344
- throw new Error(this.GetExceptionMessage('括号不匹配'))
345
- }
346
- return result
347
- }
348
- else if (t.getType() === TokenType.Oper && t.getValue() === '{') {
349
- // JSON object
350
- return this.Json()
351
- }
352
- else if (t.getType() === TokenType.Oper && t.getValue() === '[') {
353
- // JSON array
354
- return this.JsonArray()
355
- }
356
- else if (t.getType() === TokenType.Oper && (t.getValue() === '$' || t.getValue() === '"')) {
357
- // String concatenation sequence
358
- return this.StringUnion()
359
- }
360
- else if (t.getType() === TokenType.Int || t.getType() === TokenType.Double || t.getType() === TokenType.Bool) {
361
- return Expression.Constant(t.getValue(), this.pos)
362
- }
363
- else if (t.getType() === TokenType.Identy) {
364
- const objName: string = <string>t.getValue()
365
-
366
- switch (objName) {
367
- case 'return':
368
- return Expression.Return(this.Exp(), this.pos)
369
- case 'try':
370
- this.tokens.push(t) // Revert the token back to the queue
371
- return this.Try()
372
- case 'throw':
373
- return Expression.Throw(this.Exp(), this.pos)
374
- case 'validate':
375
- return this.Validate()
376
- case 'assert':
377
- return Expression.Assert(this.Exp(), this.pos)
378
- case 'break':
379
- return Expression.Break(this.pos, undefined)
380
- case 'continue':
381
- return Expression.Continue(this.pos, undefined)
382
- }
383
-
384
- // Return the object name
385
- name += objName
386
- // Process the object name
387
- return this.ObjectName(objName)
388
- }
389
- else if (t.getType() === TokenType.Null) {
390
- return Expression.Constant(null, this.pos)
391
- }
392
-
393
- throw new Error(this.GetExceptionMessage(`单词类型错误:${t.getType()}`))
394
- }
395
-
396
- /**
397
- * TryCatch := try {expression} (catch(Exception类 名){表达式})* finally{表达式}?
398
- */
399
- private Try(): Expression {
400
- let t: Token = this.GetToken()
401
- if (t.getValue() !== 'try') {
402
- throw new Error(this.GetExceptionMessage('try-catch必须以try开始'))
403
- }
404
-
405
- t = this.GetToken()
406
- if (t.getValue() !== '{') {
407
- throw new Error(this.GetExceptionMessage('try块必须以{开始'))
408
- }
409
-
410
- // Get the expression inside the try block
411
- const tryExp: Expression = this.CommaExp()
412
-
413
- t = this.GetToken()
414
- if (t.getValue() !== '}') {
415
- throw new Error(this.GetExceptionMessage('try块必须以}结束'))
416
- }
417
-
418
- // Handle the catch part
419
- const catches: Expression[] = this.Catch()
420
-
421
- // Handle finally
422
- let finallyExp: Expression | undefined
423
- t = this.GetToken()
424
- if (t.getValue()) {
425
- if (t.getValue() === 'finally') {
426
- t = this.GetToken()
427
- if (t.getValue() !== '{') {
428
- throw new Error(this.GetExceptionMessage('finally块必须以{开始'))
429
- }
430
-
431
- // Get the expression inside the finally block
432
- finallyExp = this.CommaExp()
433
- t = this.GetToken()
434
- if (t.getValue() !== '}') {
435
- throw new Error(this.GetExceptionMessage('finally块必须以}结束'))
436
- }
437
- t = this.GetToken()
438
- }
439
- }
440
-
441
- this.tokens.push(t)
442
-
443
- // Return the exception handling expression
444
- return Expression.Try(tryExp, catches, this.pos, finallyExp)
445
- }
446
-
447
- /**
448
- * catch部分处理
449
- */
450
- private Catch(): Expression[] {
451
- const result: Expression[] = []
452
- let t: Token = this.GetToken()
453
-
454
- while (t.getValue() === 'catch') {
455
- t = this.GetToken()
456
- if (t.getValue() !== '(') {
457
- throw new Error(this.GetExceptionMessage('catch块参数必须以(开始'))
458
- }
459
-
460
- // Get the class name
461
- t = this.GetToken()
462
- const className: string = <string>t.getValue()
463
-
464
- // Get the variable name
465
- t = this.GetToken()
466
- const varName: string = <string>t.getValue()
467
-
468
- t = this.GetToken()
469
- if (t.getValue() !== ')') {
470
- throw new Error(this.GetExceptionMessage('catch块参数必须以)结束'))
471
- }
472
-
473
- // Get the opening curly brace
474
- t = this.GetToken()
475
- if (t.getValue() !== '{') {
476
- throw new Error(this.GetExceptionMessage('catch块必须以{开始'))
477
- }
478
-
479
- // Get the expression inside the catch block
480
- const catchExp: Expression = this.CommaExp()
481
- result.push(Expression.Catch(className, varName, catchExp, this.pos))
482
-
483
- // Get the closing curly brace
484
- t = this.GetToken()
485
- if (t.getValue() !== '}') {
486
- throw new Error(this.GetExceptionMessage('catch块必须以}结束'))
487
- }
488
-
489
- // Get the next token
490
- t = this.GetToken()
491
- }
492
-
493
- this.tokens.push(t)
494
- return result
495
- }
496
-
497
- /**
498
- * Validate block handling
499
- */
500
- private Validate(): Expression {
501
- const t: Token = this.GetToken()
502
- if (t.getValue() !== '{') {
503
- throw new Error(this.GetExceptionMessage('validate只接收Json对象'))
504
- }
505
-
506
- // Get the expression in the validate block
507
- const exp: Expression = this.Json()
508
-
509
- // Return Validate processing expression
510
- return Expression.Validate(exp, this.pos)
511
- }
512
-
513
- /**
514
- * JSON Object ::= {} | {propertyName: propertyValue, propertyName: propertyValue}
515
- */
516
- private Json(): Expression {
517
- const children: Expression[] = []
518
-
519
- let t: Token = this.GetToken()
520
- // Empty object, return directly
521
- if (t.getType() === TokenType.Oper && t.getValue() === '}') {
522
- return Expression.Json(children, this.pos)
523
- }
524
-
525
- this.tokens.push(t)
526
- children.push(this.Attr())
527
- t = this.GetToken()
528
-
529
- // If it's a comma, continue checking the next property
530
- while (t.getType() === TokenType.Oper && t.getValue() === ',') {
531
- const nextT: Token = this.GetToken()
532
- if (nextT.getType() === TokenType.Oper && nextT.getValue() === '}') {
533
- t = nextT
534
- break
535
- }
536
- this.tokens.push(nextT)
537
- children.push(this.Attr())
538
- t = this.GetToken()
539
- }
540
-
541
- if (t.getType() !== TokenType.Oper || t.getValue() !== '}') {
542
- throw new Error(this.GetExceptionMessage('JSON对象构建错误'))
543
- }
544
-
545
- return Expression.Json(children, this.pos)
546
- }
547
-
548
- /**
549
- * JSON Array ::= []
550
- */
551
- private JsonArray(): Expression {
552
- let t: Token = this.GetToken()
553
- // Empty array, return directly
554
- if (t.getType() === TokenType.Oper && t.getValue() === ']') {
555
- // Return JSON array constant
556
- return Expression.Constant(new JSONArray(), this.pos)
557
- }
558
-
559
- // Loop to get array contents
560
- this.tokens.push(t)
561
- const ps: Expression[] = this.Params()
562
- t = this.GetToken()
563
- if (t.getType() !== TokenType.Oper || t.getValue() !== ']') {
564
- throw new Error(this.GetExceptionMessage('JSON集合构建错误'))
565
- }
566
-
567
- return Expression.Array(ps, this.pos)
568
- }
569
-
570
- /**
571
- * Attribute-Value Pair ::= propertyName: propertyValue
572
- */
573
- private Attr(): Expression {
574
- const name: Token = this.GetToken()
575
- let value: string
576
- if (name.getType() === TokenType.Identy) {
577
- value = <string>name.getValue()
578
- }
579
- else if (name.getType() === TokenType.Oper && (name.getValue() === '$' || name.getValue() === '"')) {
580
- const expression: Expression = this.StringUnion().children[1] as Expression
581
- value = <string>expression.value
582
- }
583
- else {
584
- throw new Error(this.GetExceptionMessage('JSON对象的key必须是属性名或字符串'))
585
- }
586
-
587
- const t: Token = this.GetToken()
588
- if (t.getType() !== TokenType.Oper || t.getValue() !== ':') {
589
- throw new Error(this.GetExceptionMessage('JSON构建错误'))
590
- }
591
-
592
- const exp: Expression = this.Exp()
593
- return Expression.Attr(value, exp, this.pos)
594
- }
595
-
596
- /**
597
- * Parse string concatenation sequence
598
- */
599
- private StringUnion(): Expression {
600
- let exp: Expression = Expression.Constant('', this.pos)
601
- let t: Token = this.GetToken()
602
- // Object sequence
603
- while ((t.getType() === TokenType.Oper && t.getValue() === '{') || t.getType() === TokenType.String) {
604
- // If it's a string, return the concatenated result
605
- if (t.getType() === TokenType.String) {
606
- exp = Expression.Concat(exp, Expression.Constant(t.getValue(), this.pos), this.SOURCE, this.pos)
607
- }
608
- else {
609
- // Handle content inside the {}
610
- const objExp: Expression = this.Exp()
611
- t = this.GetToken()
612
- if (t.getType() !== TokenType.Oper || t.getValue() !== '}') {
613
- throw new Error(this.GetExceptionMessage('缺少\'}\''))
614
- }
615
- // String concatenation
616
- exp = Expression.Concat(exp, objExp, this.SOURCE, this.pos)
617
- }
618
- t = this.GetToken()
619
- }
620
- this.tokens.push(t)
621
- return exp
622
- }
623
-
624
- /**
625
- * Get object and object expression by name
626
- */
627
- private ObjectName(objName: string): Expression {
628
- return Expression.Identity(objName, this.pos)
629
- }
630
-
631
- /**
632
- * Object path parsing, handles method calls, properties, and array indices
633
- */
634
- private ObjectPath(inExp: Expression): Expression {
635
- let objExp: Expression = this.ArrayIndex(inExp)
636
- let t: Token = this.GetToken()
637
- while (t.getType() === TokenType.Oper && t.getValue() === '.') {
638
- const nameToken: Token = this.GetToken()
639
- const n: Token = this.GetToken()
640
- if (n.getType() === TokenType.Oper && n.getValue() === '(') {
641
- const name: string = <string>nameToken.getValue()
642
- if (name === 'each') {
643
- objExp = this.For(objExp)
644
- }
645
- else {
646
- objExp = this.MethodCall(name, objExp)
647
- }
648
- }
649
- else {
650
- this.tokens.push(n)
651
- const pi: string = <string>nameToken.getValue()
652
- objExp = Expression.Property(objExp, pi, this.pos)
653
- objExp = this.ArrayIndex(objExp)
654
- }
655
- t = this.GetToken()
656
- }
657
- this.tokens.push(t)
658
-
659
- return objExp
660
- }
661
-
662
- /**
663
- * Array index parsing with optional condition
664
- */
665
- private ArrayIndex(objExp: Expression): Expression {
666
- let n: Token = this.GetToken()
667
- if (n.getType() === TokenType.Oper && n.getValue() === '[') {
668
- const exp: Expression = this.Exp()
669
- n = this.GetToken()
670
- if (n.getType() !== TokenType.Oper || n.getValue() !== ']') {
671
- throw new Error(this.GetExceptionMessage('缺少\']\''))
672
- }
673
- return Expression.ArrayIndex(objExp, exp, this.SOURCE, this.pos)
674
- }
675
- this.tokens.push(n)
676
- return objExp
677
- }
678
-
679
- /**
680
- * For loop handling
681
- */
682
- private For(obj: Expression): Expression {
683
- const exp: Expression = this.CommaExp()
684
- const t: Token = this.GetToken()
685
- if (t.getType() !== TokenType.Oper || t.getValue() !== ')') {
686
- throw new Error(this.GetExceptionMessage('函数调用括号不匹配'))
687
- }
688
- return Expression.For(obj, exp, this.SOURCE, this.pos)
689
- }
690
-
691
- /**
692
- * Method call handling with parameters
693
- */
694
- private MethodCall(name: string, obj: Expression): Expression {
695
- let t: Token = this.GetToken()
696
- if (t.getType() === TokenType.Oper && t.getValue() === ')') {
697
- const ps: Expression[] = []
698
- return Expression.Call(obj, name, ps, this.pos)
699
- }
700
-
701
- this.tokens.push(t)
702
- const ps: Expression[] = this.Params()
703
- t = this.GetToken()
704
- if (t.getType() !== TokenType.Oper || t.getValue() !== ')') {
705
- throw new Error(this.GetExceptionMessage('函数调用括号不匹配'))
706
- }
707
-
708
- return Expression.Call(obj, name, ps, this.pos)
709
- }
710
-
711
- /**
712
- * Function parameter list handling
713
- */
714
- private Params(): Expression[] {
715
- const ps: Expression[] = []
716
- let name: string = this.paramName()
717
- let exp: Expression = this.Exp()
718
- this.procParam(ps, exp, name)
719
- let t: Token = this.GetToken()
720
- while (t.getType() === TokenType.Oper && t.getValue() === ',') {
721
- name = this.paramName()
722
- exp = this.Exp()
723
- this.procParam(ps, exp, name)
724
- t = this.GetToken()
725
- }
726
- this.tokens.push(t)
727
- return ps
728
- }
729
-
730
- /**
731
- * Parse parameter name
732
- */
733
- private paramName(): string {
734
- const t: Token = this.GetToken()
735
- if (t.getType() === TokenType.Identy) {
736
- const t1: Token = this.GetToken()
737
- if (t1.getValue() === ':') {
738
- return <string>t.getValue()
739
- }
740
- else {
741
- this.tokens.push(t)
742
- this.tokens.push(t1)
743
- return ''
744
- }
745
- }
746
- else {
747
- this.tokens.push(t)
748
- return ''
749
- }
750
- }
751
-
752
- /**
753
- * Process parameters, especially if it contains the "data" object
754
- */
755
- private procParam(ps: Expression[], exp: Expression, name: string): void {
756
- const delegate: Delegate = this.createDelegate(exp)
757
- if (delegate.containsKey('data')) {
758
- ps.push(Expression.Constant(delegate, this.pos))
759
- }
760
- else {
761
- ps.push(Expression.Param(exp, name, this.pos))
762
- }
763
- }
764
-
765
- /**
766
- * Get exception message with position information
767
- */
768
- private GetExceptionMessage(msg: string): string {
769
- const result: string = `${this.SOURCE.substring(0, this.pos)} <- ${this.SOURCE.substring(this.pos)}`
770
- return `${msg}:\n${result}`
771
- }
772
-
773
- public GetToken(): Token {
774
- // If there are tokens in the queue, return the last one saved
775
- if (this.tokens.length > 0) {
776
- return this.tokens.shift()!
777
- }
778
-
779
- const sPos = this.pos
780
- let hasKeyword = this.pos < this.SOURCE_LENGTH
781
- let value: string | null = null
782
-
783
- if (hasKeyword) {
784
- value = this.SOURCE.charAt(this.pos)
785
- }
786
-
787
- // If it's '{', save the previous state and change to non-string state
788
- if (hasKeyword && value === '{') {
789
- this.inStrings.push(this.inString)
790
- this.inString = false
791
- const str = this.SOURCE.substring(this.pos, this.pos + 1)
792
- this.pos += 1
793
- return new Token(TokenType.Oper, str, sPos)
794
- }
795
-
796
- // If in string state, process the string
797
- if (this.inString) {
798
- const startPos = this.pos
799
- while (hasKeyword && value !== '{' && value !== '}' && value !== '$' && value !== '\"') {
800
- if (value === '\\') {
801
- const nextStrValue = this.SOURCE.charAt(this.pos + 1)
802
- if (this.pos + 1 < this.SOURCE_LENGTH && ['{', '}', '\"', '$', '\\'].includes(nextStrValue)) {
803
- this.pos++
804
- }
805
- else {
806
- break
807
- }
808
- }
809
- this.pos++
810
- if (this.pos < this.SOURCE_LENGTH) {
811
- value = this.SOURCE.charAt(this.pos)
812
- }
813
- else {
814
- hasKeyword = false
815
- }
816
- }
817
- if (!(hasKeyword && value === '{')) {
818
- this.inString = this.inStrings.pop()!
819
- }
820
- const t = new Token(TokenType.String, this.SOURCE.substring(startPos, this.pos)
821
- .replace('\\$', '$')
822
- .replace('\\{', '{')
823
- .replace('\\}', '}')
824
- .replace(/\\"/g, '"')
825
- .replace('\\\\', '\\'), sPos)
826
-
827
- if (hasKeyword && (value === '$' || value === '\"')) {
828
- this.pos++
829
- }
830
- return t
831
- }
832
-
833
- // Skip whitespace and comments
834
- while (hasKeyword && (value === ' ' || value === '\n' || value === '\t' || (this.pos < this.SOURCE_LENGTH - 2 && value === '/' && this.SOURCE.charAt(this.pos + 1) === '/'))) {
835
- if (value !== ' ' && value !== '\n' && value !== '\t') {
836
- this.pos += 2
837
- if (this.pos < this.SOURCE_LENGTH) {
838
- value = this.SOURCE.charAt(this.pos)
839
- }
840
- else {
841
- hasKeyword = false
842
- }
843
- while (hasKeyword && value !== '\n') {
844
- this.pos++
845
- if (this.pos < this.SOURCE_LENGTH) {
846
- value = this.SOURCE.charAt(this.pos)
847
- }
848
- else {
849
- hasKeyword = false
850
- }
851
- }
852
- }
853
- this.pos++
854
- if (this.pos < this.SOURCE_LENGTH) {
855
- value = this.SOURCE.charAt(this.pos)
856
- }
857
- else {
858
- hasKeyword = false
859
- }
860
- }
861
-
862
- // If we are at the end of the source, return an "End" token
863
- if (this.pos === this.SOURCE_LENGTH) {
864
- return new Token(TokenType.End, null, sPos)
865
- }
866
-
867
- if (value !== null) {
868
- // Handle number tokens
869
- if (value >= '0' && value <= '9') {
870
- const oldPos = this.pos
871
- while (hasKeyword && value >= '0' && value <= '9') {
872
- this.pos++
873
- if (this.pos < this.SOURCE_LENGTH) {
874
- value = this.SOURCE.charAt(this.pos)
875
- }
876
- else {
877
- hasKeyword = false
878
- }
879
- }
880
-
881
- if (hasKeyword && value === '.') {
882
- do {
883
- this.pos++
884
- if (this.pos < this.SOURCE_LENGTH) {
885
- value = this.SOURCE.charAt(this.pos)
886
- }
887
- else {
888
- hasKeyword = false
889
- }
890
- } while (hasKeyword && value >= '0' && value <= '9')
891
- const str = this.SOURCE.substring(oldPos, this.pos)
892
- return new Token(TokenType.Double, Number.parseFloat(str), sPos)
893
- }
894
- else {
895
- const str = this.SOURCE.substring(oldPos, this.pos)
896
- return new Token(TokenType.Int, Number.parseInt(str, 10), sPos)
897
- }
898
- }
899
-
900
- // Handle identifier tokens
901
- else if ((value >= 'a' && value <= 'z') || (value >= 'A' && value <= 'Z') || value === '_' || /[\u4E00-\u9FA5]/.test(value)) {
902
- const oldPos = this.pos
903
- while (hasKeyword && ((value >= 'a' && value <= 'z') || (value >= 'A' && value <= 'Z') || (value >= '0' && value <= '9') || value === '_' || /[\u4E00-\u9FA5]/.test(value))) {
904
- this.pos++
905
- if (this.pos < this.SOURCE_LENGTH) {
906
- value = this.SOURCE.charAt(this.pos)
907
- }
908
- else {
909
- hasKeyword = false
910
- }
911
- }
912
- const str = this.SOURCE.substring(oldPos, this.pos)
913
- switch (str) {
914
- case 'false':
915
- case 'true':
916
- return new Token(TokenType.Bool, Boolean(str), sPos)
917
- case 'null':
918
- return new Token(TokenType.Null, null, sPos)
919
- default:
920
- return new Token(TokenType.Identy, str, sPos)
921
- }
922
- }
923
-
924
- // Handle operators like &&, ||, and single-character operators
925
- else if ((value === '&' && this.SOURCE.charAt(this.pos + 1) === '&') || (value === '|' && this.SOURCE.charAt(this.pos + 1) === '|')) {
926
- const str = this.SOURCE.substring(this.pos, this.pos + 2)
927
- this.pos += 2
928
- return new Token(TokenType.Oper, str, sPos)
929
- }
930
-
931
- // Handle other operators (+, -, *, /, etc.)
932
- else if (['+', '-', '*', '/', '%', '>', '<', '!'].includes(value)) {
933
- let str = ''
934
- if (this.SOURCE.charAt(this.pos + 1) === '=') {
935
- str = this.SOURCE.substring(this.pos, this.pos + 2)
936
- this.pos += 2
937
- }
938
- else {
939
- str = this.SOURCE.substring(this.pos, this.pos + 1)
940
- this.pos += 1
941
- }
942
- return new Token(TokenType.Oper, str, sPos)
943
- }
944
-
945
- // Handle '=' with different cases
946
- else if (value === '=') {
947
- let str = ''
948
- if (this.SOURCE.charAt(this.pos + 1) === '=' || this.SOURCE.charAt(this.pos + 1) === '>') {
949
- str = this.SOURCE.substring(this.pos, this.pos + 2)
950
- this.pos += 2
951
- }
952
- else {
953
- str = this.SOURCE.substring(this.pos, this.pos + 1)
954
- this.pos += 1
955
- }
956
- return new Token(TokenType.Oper, str, sPos)
957
- }
958
-
959
- // Single character operators like parentheses, commas, etc.
960
- else if ('()[],;.:@{}"$'.includes(value)) {
961
- if (value === '$' || value === '\"') {
962
- this.inStrings.push(false)
963
- this.inString = true
964
- }
965
- else if ((value === '{' || value === '}') && this.inStrings.length) {
966
- if (value === '{') {
967
- this.inStrings.push(false)
968
- this.inString = false
969
- }
970
- else {
971
- this.inString = this.inStrings.pop()!
972
- }
973
- }
974
- const str = this.SOURCE.substring(this.pos, this.pos + 1)
975
- this.pos += 1
976
- return new Token(TokenType.Oper, str, sPos)
977
- }
978
- else {
979
- throw new Error(`Invalid token: '${value}'`)
980
- }
981
- }
982
-
983
- throw new Error('Unknown error while parsing.')
984
- }
985
- }
1
+ import ExpressionType from '../enums/ExpressionType'
2
+ import TokenType from '../enums/TokenType'
3
+ import JSONArray from '../instances/JSONArray'
4
+ import Delegate from './Delegate'
5
+ import Expression from './Expression'
6
+ import Token from './Token'
7
+
8
+ /**
9
+ * 表达式入口
10
+ */
11
+ export default class Program {
12
+ // 源程序
13
+ public readonly SOURCE: string
14
+ public readonly SOURCE_LENGTH: number
15
+ /**
16
+ * Token队列,用于回退
17
+ */
18
+ private readonly tokens: Token[] = []
19
+ /**
20
+ * 字符串处理环节堆栈,$进入字符串处理环节,“{}”中间部分脱离字符串处理环节
21
+ */
22
+ private readonly inStrings: boolean[] = []
23
+
24
+ /**
25
+ * 当前获取到的字符位置
26
+ */
27
+ private pos: number = 0
28
+
29
+ /**
30
+ * 当前是否在字符串处理环节
31
+ */
32
+ private inString: boolean = false
33
+
34
+ constructor(source: string) {
35
+ this.SOURCE = source
36
+ this.SOURCE_LENGTH = this.SOURCE.length
37
+ }
38
+
39
+ /**
40
+ * 调用解析过程
41
+ */
42
+ public parse(): Delegate {
43
+ // 执行完以下方法后就已经诞生了一个完整的执行流程,也就是被翻译成了一套完整的指令集。
44
+ return this.createDelegate(this.expression())
45
+ }
46
+
47
+ public parseExpression(expression: Expression): Delegate {
48
+ // 执行完以下方法后就已经诞生了一个完整的执行流程,也就是被翻译成了一套完整的指令集。
49
+ return this.createDelegate(expression)
50
+ }
51
+
52
+ /**
53
+ * 编译,产生可执行单元Delegate
54
+ */
55
+ public createDelegate(expression: Expression): Delegate {
56
+ return new Delegate(expression, this.SOURCE)
57
+ }
58
+
59
+ /**
60
+ * 获取完整表达式
61
+ */
62
+ public expression(): Expression {
63
+ const expression = this.CommaExp()
64
+ if (this.pos < this.SOURCE_LENGTH) {
65
+ throw new Error(this.GetExceptionMessage('需要逗号结尾!'))
66
+ }
67
+ return expression
68
+ }
69
+
70
+ /**
71
+ * 逗号表达式=赋值表达式(,赋值表达式)*
72
+ */
73
+ private CommaExp(): Expression {
74
+ const exps: Expression[] = []
75
+
76
+ const first: Expression = this.AssignExp()
77
+ exps.push(first)
78
+
79
+ let t: Token = this.GetToken()
80
+
81
+ // 没有结束
82
+ while (t.getType() === TokenType.Oper && (t.getValue() === ',' || t.getValue() === ';' || t.getValue() === '\n')) {
83
+ const r: Expression = this.AssignExp()
84
+ exps.push(r)
85
+ t = this.GetToken()
86
+ }
87
+
88
+ this.tokens.push(t)
89
+
90
+ return Expression.Comma(exps, this.pos)
91
+ }
92
+
93
+ /**
94
+ * 赋值表达式=对象属性=一般表达式|一般表达式
95
+ */
96
+ private AssignExp(): Expression {
97
+ let t: Token = this.GetToken()
98
+
99
+ // 把第一个Token的位置记录下来,方便后面回退
100
+ const firstPos: number = t.getStartPos()
101
+
102
+ if (t.getType() !== TokenType.Identy) {
103
+ this.pos = firstPos
104
+ this.tokens.length = 0 // Clear the tokens array
105
+ this.inString = false
106
+ return this.Exp()
107
+ }
108
+
109
+ let objExp: Expression = Expression.Identity(t.getValue(), this.pos)
110
+ objExp = this.ObjectPath(objExp)
111
+
112
+ // 只能给对象属性或者变量赋值
113
+ if (objExp.type !== ExpressionType.Property && objExp.type !== ExpressionType.Identity) {
114
+ this.pos = firstPos
115
+ this.tokens.length = 0
116
+ this.inString = false
117
+ return this.Exp()
118
+ }
119
+
120
+ t = this.GetToken()
121
+ if (t.getType() !== TokenType.Oper || t.getValue() !== '=') {
122
+ this.pos = firstPos
123
+ this.tokens.length = 0
124
+ this.inString = false
125
+ return this.Exp()
126
+ }
127
+
128
+ const exp: Expression = this.Exp()
129
+
130
+ // 如果是属性赋值
131
+ if (objExp.type === ExpressionType.Property) {
132
+ // 从属性Expression中获取对象及属性名
133
+ const name: string = <string>objExp.value
134
+ objExp = objExp.children[0] as Expression
135
+ return Expression.Assign(objExp, exp, name, this.pos)
136
+ }
137
+ else {
138
+ // Identity 变量赋值
139
+ return Expression.Assign(null, exp, <string>objExp.value, this.pos)
140
+ }
141
+ }
142
+
143
+ private Exp(): Expression {
144
+ const v: Expression = this.Logic()
145
+
146
+ // 是':',表示条件,否则直接返回单结果
147
+ let t: Token = this.GetToken()
148
+ if (t.getType() === TokenType.Oper && t.getValue() === ':') {
149
+ // 第一项转换
150
+ const result: Expression = this.Logic()
151
+
152
+ // 下一个是",",继续读取下一个条件结果串,由于是右结合,只能采用递归
153
+ t = this.GetToken()
154
+ if (t.getType() === TokenType.Oper && t.getValue() === ',') {
155
+ // 第二项转换
156
+ const sExp: Expression = this.Exp()
157
+ // 返回
158
+ return Expression.Condition(v, result, sExp, this.SOURCE, this.pos)
159
+ }
160
+ else {
161
+ throw new Error(this.GetExceptionMessage('必须有默认值!'))
162
+ }
163
+ }
164
+ else {
165
+ this.tokens.push(t) // Put the token back
166
+ return v
167
+ }
168
+ }
169
+
170
+ private Logic(): Expression {
171
+ let t: Token = this.GetToken()
172
+
173
+ // !表达式
174
+ const hasNot: boolean = t.getType() === TokenType.Oper && t.getValue() === '!'
175
+ if (!hasNot) {
176
+ this.tokens.push(t) // Put the token back if it's not '!'
177
+ }
178
+
179
+ let v: Expression = this.Compare()
180
+ if (hasNot) {
181
+ v = Expression.Not(v, this.SOURCE, this.pos)
182
+ }
183
+
184
+ t = this.GetToken()
185
+ while (t.getType() === TokenType.Oper && (t.getValue() === '&&' || t.getValue() === '||')) {
186
+ // 第二项转换
187
+ const exp: Expression = this.Logic()
188
+
189
+ // 执行
190
+ if (t.getValue() === '&&') {
191
+ v = Expression.And(v, exp, this.SOURCE, this.pos)
192
+ }
193
+ else {
194
+ v = Expression.Or(v, exp, this.SOURCE, this.pos)
195
+ }
196
+
197
+ t = this.GetToken()
198
+ }
199
+
200
+ this.tokens.push(t) // Put the token back
201
+ return v
202
+ }
203
+
204
+ private Compare(): Expression {
205
+ const left: Expression = this.Math()
206
+ const t: Token = this.GetToken()
207
+
208
+ if (t.getType() === TokenType.Oper && (
209
+ t.getValue() === '>' || t.getValue() === '>='
210
+ || t.getValue() === '<' || t.getValue() === '<=' || t.getValue() === '=>')) {
211
+ if (t.getValue() === '=>') {
212
+ this.tokens.push(t) // Push back the token for lambda handling
213
+ return this.Lambda()
214
+ }
215
+
216
+ const rExp: Expression = this.Math()
217
+
218
+ switch (t.getValue()) {
219
+ case '>':
220
+ return Expression.GreaterThan(left, rExp, this.SOURCE, this.pos)
221
+ case '>=':
222
+ return Expression.GreaterThanOrEqual(left, rExp, this.SOURCE, this.pos)
223
+ case '<':
224
+ return Expression.LessThan(left, rExp, this.SOURCE, this.pos)
225
+ case '<=':
226
+ return Expression.LessThanOrEqual(left, rExp, this.SOURCE, this.pos)
227
+ }
228
+ }
229
+ else if (t.getType() === TokenType.Oper && (
230
+ t.getValue() === '==' || t.getValue() === '!=')) {
231
+ const rExp: Expression = this.Math()
232
+
233
+ // 相等比较
234
+ if (t.getValue() === '==') {
235
+ return Expression.Equal(left, rExp, this.SOURCE, this.pos)
236
+ }
237
+ else {
238
+ return Expression.NotEqual(left, rExp, this.SOURCE, this.pos)
239
+ }
240
+ }
241
+
242
+ // 返回当前的表达式结果
243
+ this.tokens.push(t) // Push the token back
244
+ return left
245
+ }
246
+
247
+ private Math(): Expression {
248
+ let v: Expression = this.Mul()
249
+ let t: Token = this.GetToken()
250
+
251
+ while (t.getType() === TokenType.Oper && (t.getValue() === '+' || t.getValue() === '-')) {
252
+ // 转换操作数2为数字
253
+ const r: Expression = this.Mul()
254
+
255
+ // 开始运算
256
+ if (t.getValue() === '+') {
257
+ v = Expression.Add(v, r, this.SOURCE, this.pos)
258
+ }
259
+ else {
260
+ v = Expression.Subtract(v, r, this.SOURCE, this.pos)
261
+ }
262
+
263
+ t = this.GetToken()
264
+ }
265
+
266
+ this.tokens.push(t) // Push the token back
267
+ return v
268
+ }
269
+
270
+ private Lambda(): Expression {
271
+ let t: Token = this.GetToken()
272
+ if (t.getValue() !== '=>' || t.getValue() == null) {
273
+ throw new Error(this.GetExceptionMessage('lambda必须以=>开始'))
274
+ }
275
+
276
+ t = this.GetToken()
277
+ if (t.getValue() !== '{' || t.getValue() == null) {
278
+ throw new Error(this.GetExceptionMessage('lambda必须以{开始'))
279
+ }
280
+
281
+ // 取lambda中的表达式
282
+ const exp: Expression = this.CommaExp()
283
+
284
+ t = this.GetToken()
285
+ if (t.getValue() !== '}' || t.getValue() == null) {
286
+ throw new Error(this.GetExceptionMessage('lambda必须以}结束'))
287
+ }
288
+
289
+ return Expression.Lambda(exp, this.pos)
290
+ }
291
+
292
+ private Mul(): Expression {
293
+ let v: Expression = this.UnarySub()
294
+ let t: Token = this.GetToken()
295
+
296
+ while (t.getType() === TokenType.Oper && (t.getValue() === '*' || t.getValue() === '/' || t.getValue() === '%')) {
297
+ // Convert the second operand to a number
298
+ const r: Expression = this.UnarySub()
299
+
300
+ // Perform the operation
301
+ if (t.getValue() === '*') {
302
+ v = Expression.Multiply(v, r, this.SOURCE, this.pos)
303
+ }
304
+ else if (t.getValue() === '/') {
305
+ v = Expression.Divide(v, r, this.SOURCE, this.pos)
306
+ }
307
+ else {
308
+ v = Expression.Modulo(v, r, this.SOURCE, this.pos)
309
+ }
310
+
311
+ t = this.GetToken()
312
+ }
313
+
314
+ this.tokens.push(t)
315
+ return v
316
+ }
317
+
318
+ private UnarySub(): Expression {
319
+ const t: Token = this.GetToken()
320
+ if (t.getType() === TokenType.Oper && t.getValue() === '-') {
321
+ const r: Expression = this.Item()
322
+ return Expression.Subtract(Expression.Constant(0, this.pos), r, this.SOURCE, this.pos)
323
+ }
324
+ this.tokens.push(t) // Push the token back into the queue
325
+ return this.Item()
326
+ }
327
+
328
+ private Item(): Expression {
329
+ const objName: string = ''
330
+ // 获取对象表达式
331
+ let objExp: Expression = this.ItemHead(objName)
332
+ // 获取对象路径表达式
333
+ objExp = this.ObjectPath(objExp)
334
+ return objExp
335
+ }
336
+
337
+ private ItemHead(name: string): Expression {
338
+ let t: Token = this.GetToken()
339
+
340
+ if (t.getType() === TokenType.Oper && t.getValue() === '(') {
341
+ const result: Expression = this.CommaExp()
342
+ t = this.GetToken()
343
+ if (t.getType() !== TokenType.Oper || t.getValue() !== ')') {
344
+ throw new Error(this.GetExceptionMessage('括号不匹配'))
345
+ }
346
+ return result
347
+ }
348
+ else if (t.getType() === TokenType.Oper && t.getValue() === '{') {
349
+ // JSON object
350
+ return this.Json()
351
+ }
352
+ else if (t.getType() === TokenType.Oper && t.getValue() === '[') {
353
+ // JSON array
354
+ return this.JsonArray()
355
+ }
356
+ else if (t.getType() === TokenType.Oper && (t.getValue() === '$' || t.getValue() === '"')) {
357
+ // String concatenation sequence
358
+ return this.StringUnion()
359
+ }
360
+ else if (t.getType() === TokenType.Int || t.getType() === TokenType.Double || t.getType() === TokenType.Bool) {
361
+ return Expression.Constant(t.getValue(), this.pos)
362
+ }
363
+ else if (t.getType() === TokenType.Identy) {
364
+ const objName: string = <string>t.getValue()
365
+
366
+ switch (objName) {
367
+ case 'return':
368
+ return Expression.Return(this.Exp(), this.pos)
369
+ case 'try':
370
+ this.tokens.push(t) // Revert the token back to the queue
371
+ return this.Try()
372
+ case 'throw':
373
+ return Expression.Throw(this.Exp(), this.pos)
374
+ case 'validate':
375
+ return this.Validate()
376
+ case 'assert':
377
+ return Expression.Assert(this.Exp(), this.pos)
378
+ case 'break':
379
+ return Expression.Break(this.pos, undefined)
380
+ case 'continue':
381
+ return Expression.Continue(this.pos, undefined)
382
+ }
383
+
384
+ // Return the object name
385
+ name += objName
386
+ // Process the object name
387
+ return this.ObjectName(objName)
388
+ }
389
+ else if (t.getType() === TokenType.Null) {
390
+ return Expression.Constant(null, this.pos)
391
+ }
392
+
393
+ throw new Error(this.GetExceptionMessage(`单词类型错误:${t.getType()}`))
394
+ }
395
+
396
+ /**
397
+ * TryCatch := try {expression} (catch(Exception类 名){表达式})* finally{表达式}?
398
+ */
399
+ private Try(): Expression {
400
+ let t: Token = this.GetToken()
401
+ if (t.getValue() !== 'try') {
402
+ throw new Error(this.GetExceptionMessage('try-catch必须以try开始'))
403
+ }
404
+
405
+ t = this.GetToken()
406
+ if (t.getValue() !== '{') {
407
+ throw new Error(this.GetExceptionMessage('try块必须以{开始'))
408
+ }
409
+
410
+ // Get the expression inside the try block
411
+ const tryExp: Expression = this.CommaExp()
412
+
413
+ t = this.GetToken()
414
+ if (t.getValue() !== '}') {
415
+ throw new Error(this.GetExceptionMessage('try块必须以}结束'))
416
+ }
417
+
418
+ // Handle the catch part
419
+ const catches: Expression[] = this.Catch()
420
+
421
+ // Handle finally
422
+ let finallyExp: Expression | undefined
423
+ t = this.GetToken()
424
+ if (t.getValue()) {
425
+ if (t.getValue() === 'finally') {
426
+ t = this.GetToken()
427
+ if (t.getValue() !== '{') {
428
+ throw new Error(this.GetExceptionMessage('finally块必须以{开始'))
429
+ }
430
+
431
+ // Get the expression inside the finally block
432
+ finallyExp = this.CommaExp()
433
+ t = this.GetToken()
434
+ if (t.getValue() !== '}') {
435
+ throw new Error(this.GetExceptionMessage('finally块必须以}结束'))
436
+ }
437
+ t = this.GetToken()
438
+ }
439
+ }
440
+
441
+ this.tokens.push(t)
442
+
443
+ // Return the exception handling expression
444
+ return Expression.Try(tryExp, catches, this.pos, finallyExp)
445
+ }
446
+
447
+ /**
448
+ * catch部分处理
449
+ */
450
+ private Catch(): Expression[] {
451
+ const result: Expression[] = []
452
+ let t: Token = this.GetToken()
453
+
454
+ while (t.getValue() === 'catch') {
455
+ t = this.GetToken()
456
+ if (t.getValue() !== '(') {
457
+ throw new Error(this.GetExceptionMessage('catch块参数必须以(开始'))
458
+ }
459
+
460
+ // Get the class name
461
+ t = this.GetToken()
462
+ const className: string = <string>t.getValue()
463
+
464
+ // Get the variable name
465
+ t = this.GetToken()
466
+ const varName: string = <string>t.getValue()
467
+
468
+ t = this.GetToken()
469
+ if (t.getValue() !== ')') {
470
+ throw new Error(this.GetExceptionMessage('catch块参数必须以)结束'))
471
+ }
472
+
473
+ // Get the opening curly brace
474
+ t = this.GetToken()
475
+ if (t.getValue() !== '{') {
476
+ throw new Error(this.GetExceptionMessage('catch块必须以{开始'))
477
+ }
478
+
479
+ // Get the expression inside the catch block
480
+ const catchExp: Expression = this.CommaExp()
481
+ result.push(Expression.Catch(className, varName, catchExp, this.pos))
482
+
483
+ // Get the closing curly brace
484
+ t = this.GetToken()
485
+ if (t.getValue() !== '}') {
486
+ throw new Error(this.GetExceptionMessage('catch块必须以}结束'))
487
+ }
488
+
489
+ // Get the next token
490
+ t = this.GetToken()
491
+ }
492
+
493
+ this.tokens.push(t)
494
+ return result
495
+ }
496
+
497
+ /**
498
+ * Validate block handling
499
+ */
500
+ private Validate(): Expression {
501
+ const t: Token = this.GetToken()
502
+ if (t.getValue() !== '{') {
503
+ throw new Error(this.GetExceptionMessage('validate只接收Json对象'))
504
+ }
505
+
506
+ // Get the expression in the validate block
507
+ const exp: Expression = this.Json()
508
+
509
+ // Return Validate processing expression
510
+ return Expression.Validate(exp, this.pos)
511
+ }
512
+
513
+ /**
514
+ * JSON Object ::= {} | {propertyName: propertyValue, propertyName: propertyValue}
515
+ */
516
+ private Json(): Expression {
517
+ const children: Expression[] = []
518
+
519
+ let t: Token = this.GetToken()
520
+ // Empty object, return directly
521
+ if (t.getType() === TokenType.Oper && t.getValue() === '}') {
522
+ return Expression.Json(children, this.pos)
523
+ }
524
+
525
+ this.tokens.push(t)
526
+ children.push(this.Attr())
527
+ t = this.GetToken()
528
+
529
+ // If it's a comma, continue checking the next property
530
+ while (t.getType() === TokenType.Oper && t.getValue() === ',') {
531
+ const nextT: Token = this.GetToken()
532
+ if (nextT.getType() === TokenType.Oper && nextT.getValue() === '}') {
533
+ t = nextT
534
+ break
535
+ }
536
+ this.tokens.push(nextT)
537
+ children.push(this.Attr())
538
+ t = this.GetToken()
539
+ }
540
+
541
+ if (t.getType() !== TokenType.Oper || t.getValue() !== '}') {
542
+ throw new Error(this.GetExceptionMessage('JSON对象构建错误'))
543
+ }
544
+
545
+ return Expression.Json(children, this.pos)
546
+ }
547
+
548
+ /**
549
+ * JSON Array ::= []
550
+ */
551
+ private JsonArray(): Expression {
552
+ let t: Token = this.GetToken()
553
+ // Empty array, return directly
554
+ if (t.getType() === TokenType.Oper && t.getValue() === ']') {
555
+ // Return JSON array constant
556
+ return Expression.Constant(new JSONArray(), this.pos)
557
+ }
558
+
559
+ // Loop to get array contents
560
+ this.tokens.push(t)
561
+ const ps: Expression[] = this.Params()
562
+ t = this.GetToken()
563
+ if (t.getType() !== TokenType.Oper || t.getValue() !== ']') {
564
+ throw new Error(this.GetExceptionMessage('JSON集合构建错误'))
565
+ }
566
+
567
+ return Expression.Array(ps, this.pos)
568
+ }
569
+
570
+ /**
571
+ * Attribute-Value Pair ::= propertyName: propertyValue
572
+ */
573
+ private Attr(): Expression {
574
+ const name: Token = this.GetToken()
575
+ let value: string
576
+ if (name.getType() === TokenType.Identy) {
577
+ value = <string>name.getValue()
578
+ }
579
+ else if (name.getType() === TokenType.Oper && (name.getValue() === '$' || name.getValue() === '"')) {
580
+ const expression: Expression = this.StringUnion().children[1] as Expression
581
+ value = <string>expression.value
582
+ }
583
+ else {
584
+ throw new Error(this.GetExceptionMessage('JSON对象的key必须是属性名或字符串'))
585
+ }
586
+
587
+ const t: Token = this.GetToken()
588
+ if (t.getType() !== TokenType.Oper || t.getValue() !== ':') {
589
+ throw new Error(this.GetExceptionMessage('JSON构建错误'))
590
+ }
591
+
592
+ const exp: Expression = this.Exp()
593
+ return Expression.Attr(value, exp, this.pos)
594
+ }
595
+
596
+ /**
597
+ * Parse string concatenation sequence
598
+ */
599
+ private StringUnion(): Expression {
600
+ let exp: Expression = Expression.Constant('', this.pos)
601
+ let t: Token = this.GetToken()
602
+ // Object sequence
603
+ while ((t.getType() === TokenType.Oper && t.getValue() === '{') || t.getType() === TokenType.String) {
604
+ // If it's a string, return the concatenated result
605
+ if (t.getType() === TokenType.String) {
606
+ exp = Expression.Concat(exp, Expression.Constant(t.getValue(), this.pos), this.SOURCE, this.pos)
607
+ }
608
+ else {
609
+ // Handle content inside the {}
610
+ const objExp: Expression = this.Exp()
611
+ t = this.GetToken()
612
+ if (t.getType() !== TokenType.Oper || t.getValue() !== '}') {
613
+ throw new Error(this.GetExceptionMessage('缺少\'}\''))
614
+ }
615
+ // String concatenation
616
+ exp = Expression.Concat(exp, objExp, this.SOURCE, this.pos)
617
+ }
618
+ t = this.GetToken()
619
+ }
620
+ this.tokens.push(t)
621
+ return exp
622
+ }
623
+
624
+ /**
625
+ * Get object and object expression by name
626
+ */
627
+ private ObjectName(objName: string): Expression {
628
+ return Expression.Identity(objName, this.pos)
629
+ }
630
+
631
+ /**
632
+ * Object path parsing, handles method calls, properties, and array indices
633
+ */
634
+ private ObjectPath(inExp: Expression): Expression {
635
+ let objExp: Expression = this.ArrayIndex(inExp)
636
+ let t: Token = this.GetToken()
637
+ while (t.getType() === TokenType.Oper && t.getValue() === '.') {
638
+ const nameToken: Token = this.GetToken()
639
+ const n: Token = this.GetToken()
640
+ if (n.getType() === TokenType.Oper && n.getValue() === '(') {
641
+ const name: string = <string>nameToken.getValue()
642
+ if (name === 'each') {
643
+ objExp = this.For(objExp)
644
+ }
645
+ else {
646
+ objExp = this.MethodCall(name, objExp)
647
+ }
648
+ }
649
+ else {
650
+ this.tokens.push(n)
651
+ const pi: string = <string>nameToken.getValue()
652
+ objExp = Expression.Property(objExp, pi, this.pos)
653
+ objExp = this.ArrayIndex(objExp)
654
+ }
655
+ t = this.GetToken()
656
+ }
657
+ this.tokens.push(t)
658
+
659
+ return objExp
660
+ }
661
+
662
+ /**
663
+ * Array index parsing with optional condition
664
+ */
665
+ private ArrayIndex(objExp: Expression): Expression {
666
+ let n: Token = this.GetToken()
667
+ if (n.getType() === TokenType.Oper && n.getValue() === '[') {
668
+ const exp: Expression = this.Exp()
669
+ n = this.GetToken()
670
+ if (n.getType() !== TokenType.Oper || n.getValue() !== ']') {
671
+ throw new Error(this.GetExceptionMessage('缺少\']\''))
672
+ }
673
+ return Expression.ArrayIndex(objExp, exp, this.SOURCE, this.pos)
674
+ }
675
+ this.tokens.push(n)
676
+ return objExp
677
+ }
678
+
679
+ /**
680
+ * For loop handling
681
+ */
682
+ private For(obj: Expression): Expression {
683
+ const exp: Expression = this.CommaExp()
684
+ const t: Token = this.GetToken()
685
+ if (t.getType() !== TokenType.Oper || t.getValue() !== ')') {
686
+ throw new Error(this.GetExceptionMessage('函数调用括号不匹配'))
687
+ }
688
+ return Expression.For(obj, exp, this.SOURCE, this.pos)
689
+ }
690
+
691
+ /**
692
+ * Method call handling with parameters
693
+ */
694
+ private MethodCall(name: string, obj: Expression): Expression {
695
+ let t: Token = this.GetToken()
696
+ if (t.getType() === TokenType.Oper && t.getValue() === ')') {
697
+ const ps: Expression[] = []
698
+ return Expression.Call(obj, name, ps, this.pos)
699
+ }
700
+
701
+ this.tokens.push(t)
702
+ const ps: Expression[] = this.Params()
703
+ t = this.GetToken()
704
+ if (t.getType() !== TokenType.Oper || t.getValue() !== ')') {
705
+ throw new Error(this.GetExceptionMessage('函数调用括号不匹配'))
706
+ }
707
+
708
+ return Expression.Call(obj, name, ps, this.pos)
709
+ }
710
+
711
+ /**
712
+ * Function parameter list handling
713
+ */
714
+ private Params(): Expression[] {
715
+ const ps: Expression[] = []
716
+ let name: string = this.paramName()
717
+ let exp: Expression = this.Exp()
718
+ this.procParam(ps, exp, name)
719
+ let t: Token = this.GetToken()
720
+ while (t.getType() === TokenType.Oper && t.getValue() === ',') {
721
+ name = this.paramName()
722
+ exp = this.Exp()
723
+ this.procParam(ps, exp, name)
724
+ t = this.GetToken()
725
+ }
726
+ this.tokens.push(t)
727
+ return ps
728
+ }
729
+
730
+ /**
731
+ * Parse parameter name
732
+ */
733
+ private paramName(): string {
734
+ const t: Token = this.GetToken()
735
+ if (t.getType() === TokenType.Identy) {
736
+ const t1: Token = this.GetToken()
737
+ if (t1.getValue() === ':') {
738
+ return <string>t.getValue()
739
+ }
740
+ else {
741
+ this.tokens.push(t)
742
+ this.tokens.push(t1)
743
+ return ''
744
+ }
745
+ }
746
+ else {
747
+ this.tokens.push(t)
748
+ return ''
749
+ }
750
+ }
751
+
752
+ /**
753
+ * Process parameters, especially if it contains the "data" object
754
+ */
755
+ private procParam(ps: Expression[], exp: Expression, name: string): void {
756
+ const delegate: Delegate = this.createDelegate(exp)
757
+ if (delegate.containsKey('data')) {
758
+ ps.push(Expression.Constant(delegate, this.pos))
759
+ }
760
+ else {
761
+ ps.push(Expression.Param(exp, name, this.pos))
762
+ }
763
+ }
764
+
765
+ /**
766
+ * Get exception message with position information
767
+ */
768
+ private GetExceptionMessage(msg: string): string {
769
+ const result: string = `${this.SOURCE.substring(0, this.pos)} <- ${this.SOURCE.substring(this.pos)}`
770
+ return `${msg}:\n${result}`
771
+ }
772
+
773
+ public GetToken(): Token {
774
+ // If there are tokens in the queue, return the last one saved
775
+ if (this.tokens.length > 0) {
776
+ return this.tokens.shift()!
777
+ }
778
+
779
+ const sPos = this.pos
780
+ let hasKeyword = this.pos < this.SOURCE_LENGTH
781
+ let value: string | null = null
782
+
783
+ if (hasKeyword) {
784
+ value = this.SOURCE.charAt(this.pos)
785
+ }
786
+
787
+ // If it's '{', save the previous state and change to non-string state
788
+ if (hasKeyword && value === '{') {
789
+ this.inStrings.push(this.inString)
790
+ this.inString = false
791
+ const str = this.SOURCE.substring(this.pos, this.pos + 1)
792
+ this.pos += 1
793
+ return new Token(TokenType.Oper, str, sPos)
794
+ }
795
+
796
+ // If in string state, process the string
797
+ if (this.inString) {
798
+ const startPos = this.pos
799
+ while (hasKeyword && value !== '{' && value !== '}' && value !== '$' && value !== '\"') {
800
+ if (value === '\\') {
801
+ const nextStrValue = this.SOURCE.charAt(this.pos + 1)
802
+ if (this.pos + 1 < this.SOURCE_LENGTH && ['{', '}', '\"', '$', '\\'].includes(nextStrValue)) {
803
+ this.pos++
804
+ }
805
+ else {
806
+ break
807
+ }
808
+ }
809
+ this.pos++
810
+ if (this.pos < this.SOURCE_LENGTH) {
811
+ value = this.SOURCE.charAt(this.pos)
812
+ }
813
+ else {
814
+ hasKeyword = false
815
+ }
816
+ }
817
+ if (!(hasKeyword && value === '{')) {
818
+ this.inString = this.inStrings.pop()!
819
+ }
820
+ const t = new Token(TokenType.String, this.SOURCE.substring(startPos, this.pos)
821
+ .replace('\\$', '$')
822
+ .replace('\\{', '{')
823
+ .replace('\\}', '}')
824
+ .replace(/\\"/g, '"')
825
+ .replace('\\\\', '\\'), sPos)
826
+
827
+ if (hasKeyword && (value === '$' || value === '\"')) {
828
+ this.pos++
829
+ }
830
+ return t
831
+ }
832
+
833
+ // Skip whitespace and comments
834
+ while (hasKeyword && (value === ' ' || value === '\n' || value === '\t' || (this.pos < this.SOURCE_LENGTH - 2 && value === '/' && this.SOURCE.charAt(this.pos + 1) === '/'))) {
835
+ if (value !== ' ' && value !== '\n' && value !== '\t') {
836
+ this.pos += 2
837
+ if (this.pos < this.SOURCE_LENGTH) {
838
+ value = this.SOURCE.charAt(this.pos)
839
+ }
840
+ else {
841
+ hasKeyword = false
842
+ }
843
+ while (hasKeyword && value !== '\n') {
844
+ this.pos++
845
+ if (this.pos < this.SOURCE_LENGTH) {
846
+ value = this.SOURCE.charAt(this.pos)
847
+ }
848
+ else {
849
+ hasKeyword = false
850
+ }
851
+ }
852
+ }
853
+ this.pos++
854
+ if (this.pos < this.SOURCE_LENGTH) {
855
+ value = this.SOURCE.charAt(this.pos)
856
+ }
857
+ else {
858
+ hasKeyword = false
859
+ }
860
+ }
861
+
862
+ // If we are at the end of the source, return an "End" token
863
+ if (this.pos === this.SOURCE_LENGTH) {
864
+ return new Token(TokenType.End, null, sPos)
865
+ }
866
+
867
+ if (value !== null) {
868
+ // Handle number tokens
869
+ if (value >= '0' && value <= '9') {
870
+ const oldPos = this.pos
871
+ while (hasKeyword && value >= '0' && value <= '9') {
872
+ this.pos++
873
+ if (this.pos < this.SOURCE_LENGTH) {
874
+ value = this.SOURCE.charAt(this.pos)
875
+ }
876
+ else {
877
+ hasKeyword = false
878
+ }
879
+ }
880
+
881
+ if (hasKeyword && value === '.') {
882
+ do {
883
+ this.pos++
884
+ if (this.pos < this.SOURCE_LENGTH) {
885
+ value = this.SOURCE.charAt(this.pos)
886
+ }
887
+ else {
888
+ hasKeyword = false
889
+ }
890
+ } while (hasKeyword && value >= '0' && value <= '9')
891
+ const str = this.SOURCE.substring(oldPos, this.pos)
892
+ return new Token(TokenType.Double, Number.parseFloat(str), sPos)
893
+ }
894
+ else {
895
+ const str = this.SOURCE.substring(oldPos, this.pos)
896
+ return new Token(TokenType.Int, Number.parseInt(str, 10), sPos)
897
+ }
898
+ }
899
+
900
+ // Handle identifier tokens
901
+ else if ((value >= 'a' && value <= 'z') || (value >= 'A' && value <= 'Z') || value === '_' || /[\u4E00-\u9FA5]/.test(value)) {
902
+ const oldPos = this.pos
903
+ while (hasKeyword && ((value >= 'a' && value <= 'z') || (value >= 'A' && value <= 'Z') || (value >= '0' && value <= '9') || value === '_' || /[\u4E00-\u9FA5]/.test(value))) {
904
+ this.pos++
905
+ if (this.pos < this.SOURCE_LENGTH) {
906
+ value = this.SOURCE.charAt(this.pos)
907
+ }
908
+ else {
909
+ hasKeyword = false
910
+ }
911
+ }
912
+ const str = this.SOURCE.substring(oldPos, this.pos)
913
+ switch (str) {
914
+ case 'false':
915
+ case 'true':
916
+ return new Token(TokenType.Bool, Boolean(str), sPos)
917
+ case 'null':
918
+ return new Token(TokenType.Null, null, sPos)
919
+ default:
920
+ return new Token(TokenType.Identy, str, sPos)
921
+ }
922
+ }
923
+
924
+ // Handle operators like &&, ||, and single-character operators
925
+ else if ((value === '&' && this.SOURCE.charAt(this.pos + 1) === '&') || (value === '|' && this.SOURCE.charAt(this.pos + 1) === '|')) {
926
+ const str = this.SOURCE.substring(this.pos, this.pos + 2)
927
+ this.pos += 2
928
+ return new Token(TokenType.Oper, str, sPos)
929
+ }
930
+
931
+ // Handle other operators (+, -, *, /, etc.)
932
+ else if (['+', '-', '*', '/', '%', '>', '<', '!'].includes(value)) {
933
+ let str = ''
934
+ if (this.SOURCE.charAt(this.pos + 1) === '=') {
935
+ str = this.SOURCE.substring(this.pos, this.pos + 2)
936
+ this.pos += 2
937
+ }
938
+ else {
939
+ str = this.SOURCE.substring(this.pos, this.pos + 1)
940
+ this.pos += 1
941
+ }
942
+ return new Token(TokenType.Oper, str, sPos)
943
+ }
944
+
945
+ // Handle '=' with different cases
946
+ else if (value === '=') {
947
+ let str = ''
948
+ if (this.SOURCE.charAt(this.pos + 1) === '=' || this.SOURCE.charAt(this.pos + 1) === '>') {
949
+ str = this.SOURCE.substring(this.pos, this.pos + 2)
950
+ this.pos += 2
951
+ }
952
+ else {
953
+ str = this.SOURCE.substring(this.pos, this.pos + 1)
954
+ this.pos += 1
955
+ }
956
+ return new Token(TokenType.Oper, str, sPos)
957
+ }
958
+
959
+ // Single character operators like parentheses, commas, etc.
960
+ else if ('()[],;.:@{}"$'.includes(value)) {
961
+ if (value === '$' || value === '\"') {
962
+ this.inStrings.push(false)
963
+ this.inString = true
964
+ }
965
+ else if ((value === '{' || value === '}') && this.inStrings.length) {
966
+ if (value === '{') {
967
+ this.inStrings.push(false)
968
+ this.inString = false
969
+ }
970
+ else {
971
+ this.inString = this.inStrings.pop()!
972
+ }
973
+ }
974
+ const str = this.SOURCE.substring(this.pos, this.pos + 1)
975
+ this.pos += 1
976
+ return new Token(TokenType.Oper, str, sPos)
977
+ }
978
+ else {
979
+ throw new Error(`Invalid token: '${value}'`)
980
+ }
981
+ }
982
+
983
+ throw new Error('Unknown error while parsing.')
984
+ }
985
+ }