@tdesign-react/chat 1.0.2-alpha.8 → 1.1.0-alpha.1
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.
- package/es/_chunks/dep-f2051ebd.js +839 -0
- package/es/_chunks/dep-f2051ebd.js.map +1 -0
- package/es/_util/reactify.d.ts +1 -2
- package/es/_util/reactify.js +81 -231
- package/es/_util/reactify.js.map +1 -1
- package/es/_util/useDynamicStyle.js +1 -1
- package/es/attachments/index.js +4 -6
- package/es/attachments/index.js.map +1 -1
- package/es/chat-actionbar/_example-js/base.d.ts +2 -0
- package/es/chat-actionbar/_example-js/custom.d.ts +2 -0
- package/es/chat-actionbar/_example-js/style.d.ts +2 -0
- package/es/chat-actionbar/index.js +4 -6
- package/es/chat-actionbar/index.js.map +1 -1
- package/es/chat-attachments/_example-js/base.d.ts +2 -0
- package/es/chat-attachments/_example-js/scroll-x.d.ts +2 -0
- package/es/chat-attachments/_example-js/scroll-y.d.ts +2 -0
- package/es/chat-attachments/index.js +3 -1
- package/es/chat-attachments/index.js.map +1 -1
- package/es/chat-engine/_example-js/agui-basic.d.ts +10 -0
- package/es/chat-engine/_example-js/agui-comprehensive.d.ts +2 -0
- package/es/chat-engine/_example-js/agui-test.d.ts +11 -0
- package/es/chat-engine/_example-js/agui-toolcall.d.ts +5 -0
- package/es/chat-engine/_example-js/agui-videoclip.d.ts +8 -0
- package/es/chat-engine/_example-js/agui.d.ts +1 -0
- package/es/chat-engine/_example-js/basic.d.ts +9 -0
- package/es/chat-engine/_example-js/components/HotelCard.d.ts +3 -0
- package/es/chat-engine/_example-js/components/HumanInputForm.d.ts +6 -0
- package/es/chat-engine/_example-js/components/HumanInputResult.d.ts +3 -0
- package/es/chat-engine/_example-js/components/ItineraryCard.d.ts +3 -0
- package/es/chat-engine/_example-js/components/PlanningStatePanel.d.ts +4 -0
- package/es/chat-engine/_example-js/components/Toolcall.d.ts +4 -0
- package/es/chat-engine/_example-js/components/WeatherCard.d.ts +3 -0
- package/es/chat-engine/_example-js/components/login.d.ts +1 -0
- package/es/chat-engine/_example-js/comprehensive.d.ts +12 -0
- package/es/chat-engine/_example-js/custom-content.d.ts +1 -0
- package/es/chat-engine/_example-js/hookComponent.d.ts +1 -0
- package/es/chat-engine/_example-js/initial-messages.d.ts +9 -0
- package/es/chat-engine/_example-js/instance-methods.d.ts +13 -0
- package/es/chat-engine/_example-js/travel-actions.d.ts +91 -0
- package/es/chat-engine/_example-js/travelToolcall.d.ts +1 -0
- package/es/chat-engine/components/a2ui/A2UIActivityRenderer.d.ts +196 -0
- package/es/chat-engine/components/a2ui/A2UIActivityRenderer.js +301 -0
- package/es/chat-engine/components/a2ui/A2UIActivityRenderer.js.map +1 -0
- package/es/chat-engine/components/a2ui/A2UIComponentRenderer.d.ts +37 -0
- package/es/chat-engine/components/a2ui/A2UIComponentRenderer.js +116 -0
- package/es/chat-engine/components/a2ui/A2UIComponentRenderer.js.map +1 -0
- package/es/chat-engine/components/a2ui/A2UIErrorBoundary.d.ts +42 -0
- package/es/chat-engine/components/a2ui/A2UIErrorBoundary.js +113 -0
- package/es/chat-engine/components/a2ui/A2UIErrorBoundary.js.map +1 -0
- package/es/chat-engine/components/a2ui/A2UISkeleton.d.ts +60 -0
- package/es/chat-engine/components/a2ui/A2UISkeleton.js +197 -0
- package/es/chat-engine/components/a2ui/A2UISkeleton.js.map +1 -0
- package/es/chat-engine/components/a2ui/A2UISurfaceRenderer.d.ts +44 -0
- package/es/chat-engine/components/a2ui/A2UISurfaceRenderer.js +139 -0
- package/es/chat-engine/components/a2ui/A2UISurfaceRenderer.js.map +1 -0
- package/es/chat-engine/components/a2ui/containers/A2UICard.d.ts +22 -0
- package/es/chat-engine/components/a2ui/containers/A2UICard.js +82 -0
- package/es/chat-engine/components/a2ui/containers/A2UICard.js.map +1 -0
- package/es/chat-engine/components/a2ui/containers/A2UIColumn.d.ts +18 -0
- package/es/chat-engine/components/a2ui/containers/A2UIColumn.js +81 -0
- package/es/chat-engine/components/a2ui/containers/A2UIColumn.js.map +1 -0
- package/es/chat-engine/components/a2ui/containers/A2UIList.d.ts +14 -0
- package/es/chat-engine/components/a2ui/containers/A2UIList.js +62 -0
- package/es/chat-engine/components/a2ui/containers/A2UIList.js.map +1 -0
- package/es/chat-engine/components/a2ui/containers/A2UIRow.d.ts +20 -0
- package/es/chat-engine/components/a2ui/containers/A2UIRow.js +84 -0
- package/es/chat-engine/components/a2ui/containers/A2UIRow.js.map +1 -0
- package/es/chat-engine/components/a2ui/containers/A2UITabs.d.ts +14 -0
- package/es/chat-engine/components/a2ui/containers/A2UITabs.js +69 -0
- package/es/chat-engine/components/a2ui/containers/A2UITabs.js.map +1 -0
- package/es/chat-engine/components/a2ui/containers/index.d.ts +8 -0
- package/es/chat-engine/components/a2ui/containers/index.js +30 -0
- package/es/chat-engine/components/a2ui/containers/index.js.map +1 -0
- package/es/chat-engine/components/a2ui/index.d.ts +11 -0
- package/es/chat-engine/components/a2ui/index.js +62 -0
- package/es/chat-engine/components/a2ui/index.js.map +1 -0
- package/es/chat-engine/components/a2ui/primitives/A2UIButton.d.ts +33 -0
- package/es/chat-engine/components/a2ui/primitives/A2UIButton.js +80 -0
- package/es/chat-engine/components/a2ui/primitives/A2UIButton.js.map +1 -0
- package/es/chat-engine/components/a2ui/primitives/A2UICheckBox.d.ts +12 -0
- package/es/chat-engine/components/a2ui/primitives/A2UICheckBox.js +43 -0
- package/es/chat-engine/components/a2ui/primitives/A2UICheckBox.js.map +1 -0
- package/es/chat-engine/components/a2ui/primitives/A2UICheckboxGroup.d.ts +50 -0
- package/es/chat-engine/components/a2ui/primitives/A2UICheckboxGroup.js +54 -0
- package/es/chat-engine/components/a2ui/primitives/A2UICheckboxGroup.js.map +1 -0
- package/es/chat-engine/components/a2ui/primitives/A2UIChoicePicker.d.ts +13 -0
- package/es/chat-engine/components/a2ui/primitives/A2UIChoicePicker.js +89 -0
- package/es/chat-engine/components/a2ui/primitives/A2UIChoicePicker.js.map +1 -0
- package/es/chat-engine/components/a2ui/primitives/A2UIDivider.d.ts +12 -0
- package/es/chat-engine/components/a2ui/primitives/A2UIDivider.js +25 -0
- package/es/chat-engine/components/a2ui/primitives/A2UIDivider.js.map +1 -0
- package/es/chat-engine/components/a2ui/primitives/A2UIIcon.d.ts +13 -0
- package/es/chat-engine/components/a2ui/primitives/A2UIIcon.js +138942 -0
- package/es/chat-engine/components/a2ui/primitives/A2UIIcon.js.map +1 -0
- package/es/chat-engine/components/a2ui/primitives/A2UIImage.d.ts +12 -0
- package/es/chat-engine/components/a2ui/primitives/A2UIImage.js +75 -0
- package/es/chat-engine/components/a2ui/primitives/A2UIImage.js.map +1 -0
- package/es/chat-engine/components/a2ui/primitives/A2UIInput.d.ts +20 -0
- package/es/chat-engine/components/a2ui/primitives/A2UIInput.js +63 -0
- package/es/chat-engine/components/a2ui/primitives/A2UIInput.js.map +1 -0
- package/es/chat-engine/components/a2ui/primitives/A2UIRadioGroup.d.ts +56 -0
- package/es/chat-engine/components/a2ui/primitives/A2UIRadioGroup.js +64 -0
- package/es/chat-engine/components/a2ui/primitives/A2UIRadioGroup.js.map +1 -0
- package/es/chat-engine/components/a2ui/primitives/A2UISlider.d.ts +12 -0
- package/es/chat-engine/components/a2ui/primitives/A2UISlider.js +55 -0
- package/es/chat-engine/components/a2ui/primitives/A2UISlider.js.map +1 -0
- package/es/chat-engine/components/a2ui/primitives/A2UIText.d.ts +12 -0
- package/es/chat-engine/components/a2ui/primitives/A2UIText.js +104 -0
- package/es/chat-engine/components/a2ui/primitives/A2UIText.js.map +1 -0
- package/es/chat-engine/components/a2ui/primitives/A2UITextField.d.ts +12 -0
- package/es/chat-engine/components/a2ui/primitives/A2UITextField.js +80 -0
- package/es/chat-engine/components/a2ui/primitives/A2UITextField.js.map +1 -0
- package/es/chat-engine/components/a2ui/primitives/index.d.ts +15 -0
- package/es/chat-engine/components/a2ui/primitives/index.js +39 -0
- package/es/chat-engine/components/a2ui/primitives/index.js.map +1 -0
- package/es/chat-engine/components/a2ui/registry.d.ts +35 -0
- package/es/chat-engine/components/a2ui/registry.js +74 -0
- package/es/chat-engine/components/a2ui/registry.js.map +1 -0
- package/es/{chatbot/components/toolcall → chat-engine/components/activity}/index.d.ts +1 -0
- package/es/chat-engine/components/activity/index.js +18 -0
- package/es/chat-engine/components/activity/index.js.map +1 -0
- package/es/chat-engine/components/activity/registry.d.ts +44 -0
- package/es/{chatbot/components/toolcall → chat-engine/components/activity}/registry.js +32 -22
- package/es/chat-engine/components/activity/registry.js.map +1 -0
- package/es/chat-engine/components/activity/render.d.ts +11 -0
- package/es/chat-engine/components/activity/render.js +107 -0
- package/es/chat-engine/components/activity/render.js.map +1 -0
- package/es/chat-engine/components/activity/types.d.ts +32 -0
- package/es/chat-engine/components/activity/types.js +7 -0
- package/es/chat-engine/components/activity/types.js.map +1 -0
- package/es/chat-engine/components/index.d.ts +4 -0
- package/es/chat-engine/components/index.js +76 -0
- package/es/chat-engine/components/index.js.map +1 -0
- package/es/chat-engine/components/json-render/A2UIJsonRenderActivityRenderer.d.ts +36 -0
- package/es/chat-engine/components/json-render/A2UIJsonRenderActivityRenderer.js +36 -0
- package/es/chat-engine/components/json-render/A2UIJsonRenderActivityRenderer.js.map +1 -0
- package/es/chat-engine/components/json-render/JsonRenderActivityRenderer.d.ts +46 -0
- package/es/chat-engine/components/json-render/JsonRenderActivityRenderer.js +36 -0
- package/es/chat-engine/components/json-render/JsonRenderActivityRenderer.js.map +1 -0
- package/es/chat-engine/components/json-render/SurfaceStateManager.d.ts +113 -0
- package/es/chat-engine/components/json-render/SurfaceStateManager.js +195 -0
- package/es/chat-engine/components/json-render/SurfaceStateManager.js.map +1 -0
- package/es/chat-engine/components/json-render/adapters/a2ui-to-jsonrender.d.ts +49 -0
- package/es/chat-engine/components/json-render/adapters/a2ui-to-jsonrender.js +307 -0
- package/es/chat-engine/components/json-render/adapters/a2ui-to-jsonrender.js.map +1 -0
- package/es/chat-engine/components/json-render/adapters/a2ui-types.d.ts +96 -0
- package/es/chat-engine/components/json-render/adapters/a2ui-types.js +7 -0
- package/es/chat-engine/components/json-render/adapters/a2ui-types.js.map +1 -0
- package/es/chat-engine/components/json-render/adapters/index.d.ts +7 -0
- package/es/chat-engine/components/json-render/adapters/index.js +11 -0
- package/es/chat-engine/components/json-render/adapters/index.js.map +1 -0
- package/es/chat-engine/components/json-render/catalog/a2ui-binding.d.ts +64 -0
- package/es/chat-engine/components/json-render/catalog/a2ui-binding.js +36 -0
- package/es/chat-engine/components/json-render/catalog/a2ui-binding.js.map +1 -0
- package/es/chat-engine/components/json-render/catalog/a2ui-registry.d.ts +78 -0
- package/es/chat-engine/components/json-render/catalog/a2ui-registry.js +36 -0
- package/es/chat-engine/components/json-render/catalog/a2ui-registry.js.map +1 -0
- package/es/chat-engine/components/json-render/catalog/button.d.ts +14 -0
- package/es/chat-engine/components/json-render/catalog/button.js +71 -0
- package/es/chat-engine/components/json-render/catalog/button.js.map +1 -0
- package/es/chat-engine/components/json-render/catalog/card.d.ts +11 -0
- package/es/chat-engine/components/json-render/catalog/card.js +57 -0
- package/es/chat-engine/components/json-render/catalog/card.js.map +1 -0
- package/es/chat-engine/components/json-render/catalog/index.d.ts +127 -0
- package/es/chat-engine/components/json-render/catalog/index.js +36 -0
- package/es/chat-engine/components/json-render/catalog/index.js.map +1 -0
- package/es/chat-engine/components/json-render/catalog/input.d.ts +20 -0
- package/es/chat-engine/components/json-render/catalog/input.js +36 -0
- package/es/chat-engine/components/json-render/catalog/input.js.map +1 -0
- package/es/chat-engine/components/json-render/catalog/layout.d.ts +44 -0
- package/es/chat-engine/components/json-render/catalog/layout.js +129 -0
- package/es/chat-engine/components/json-render/catalog/layout.js.map +1 -0
- package/es/chat-engine/components/json-render/catalog/text.d.ts +11 -0
- package/es/chat-engine/components/json-render/catalog/text.js +31 -0
- package/es/chat-engine/components/json-render/catalog/text.js.map +1 -0
- package/es/chat-engine/components/json-render/catalog.d.ts +549 -0
- package/es/chat-engine/components/json-render/catalog.js +147 -0
- package/es/chat-engine/components/json-render/catalog.js.map +1 -0
- package/es/chat-engine/components/json-render/config.d.ts +154 -0
- package/es/chat-engine/components/json-render/config.js +36 -0
- package/es/chat-engine/components/json-render/config.js.map +1 -0
- package/es/chat-engine/components/json-render/contexts/actions.d.ts +74 -0
- package/es/chat-engine/components/json-render/contexts/actions.js +290 -0
- package/es/chat-engine/components/json-render/contexts/actions.js.map +1 -0
- package/es/chat-engine/components/json-render/contexts/data.d.ts +45 -0
- package/es/chat-engine/components/json-render/contexts/data.js +104 -0
- package/es/chat-engine/components/json-render/contexts/data.js.map +1 -0
- package/es/chat-engine/components/json-render/contexts/validation.d.ts +59 -0
- package/es/chat-engine/components/json-render/contexts/validation.js +151 -0
- package/es/chat-engine/components/json-render/contexts/validation.js.map +1 -0
- package/es/chat-engine/components/json-render/contexts/visibility.d.ts +29 -0
- package/es/chat-engine/components/json-render/contexts/visibility.js +56 -0
- package/es/chat-engine/components/json-render/contexts/visibility.js.map +1 -0
- package/es/chat-engine/components/json-render/engine.d.ts +58 -0
- package/es/chat-engine/components/json-render/engine.js +88 -0
- package/es/chat-engine/components/json-render/engine.js.map +1 -0
- package/es/chat-engine/components/json-render/index.d.ts +34 -0
- package/es/chat-engine/components/json-render/index.js +36 -0
- package/es/chat-engine/components/json-render/index.js.map +1 -0
- package/es/chat-engine/components/json-render/performance.d.ts +91 -0
- package/es/chat-engine/components/json-render/performance.js +135 -0
- package/es/chat-engine/components/json-render/performance.js.map +1 -0
- package/es/chat-engine/components/json-render/renderer.d.ts +71 -0
- package/es/chat-engine/components/json-render/renderer.js +124 -0
- package/es/chat-engine/components/json-render/renderer.js.map +1 -0
- package/es/chat-engine/components/json-render/types.d.ts +69 -0
- package/es/chat-engine/components/json-render/types.js +7 -0
- package/es/chat-engine/components/json-render/types.js.map +1 -0
- package/es/chat-engine/components/provider/agent-state.js +39 -2
- package/es/chat-engine/components/provider/agent-state.js.map +1 -1
- package/es/chat-engine/components/toolcall/index.js +35 -2
- package/es/chat-engine/components/toolcall/index.js.map +1 -1
- package/es/chat-engine/components/toolcall/registry.js +1 -1
- package/es/chat-engine/components/toolcall/render.d.ts +1 -1
- package/es/chat-engine/components/toolcall/render.js +102 -26
- package/es/chat-engine/components/toolcall/render.js.map +1 -1
- package/es/chat-engine/components/toolcall/types.js +1 -1
- package/es/chat-engine/core/a2ui/index.d.ts +10 -0
- package/es/chat-engine/core/a2ui/index.js +24 -0
- package/es/chat-engine/core/a2ui/index.js.map +1 -0
- package/es/chat-engine/core/a2ui/processor/A2uiMessageProcessor.d.ts +197 -0
- package/es/chat-engine/core/a2ui/processor/A2uiMessageProcessor.js +438 -0
- package/es/chat-engine/core/a2ui/processor/A2uiMessageProcessor.js.map +1 -0
- package/es/chat-engine/core/a2ui/processor/ComponentTree.d.ts +53 -0
- package/es/chat-engine/core/a2ui/processor/ComponentTree.js +158 -0
- package/es/chat-engine/core/a2ui/processor/ComponentTree.js.map +1 -0
- package/es/chat-engine/core/a2ui/processor/DataStore.d.ts +63 -0
- package/es/chat-engine/core/a2ui/processor/DataStore.js +172 -0
- package/es/chat-engine/core/a2ui/processor/DataStore.js.map +1 -0
- package/es/chat-engine/core/a2ui/processor/PathResolver.d.ts +47 -0
- package/es/chat-engine/core/a2ui/processor/PathResolver.js +99 -0
- package/es/chat-engine/core/a2ui/processor/PathResolver.js.map +1 -0
- package/es/chat-engine/core/a2ui/types/index.d.ts +4 -0
- package/es/chat-engine/core/a2ui/types/index.js +9 -0
- package/es/chat-engine/core/a2ui/types/index.js.map +1 -0
- package/es/chat-engine/core/a2ui/types/types.d.ts +770 -0
- package/es/chat-engine/core/a2ui/types/types.js +104 -0
- package/es/chat-engine/core/a2ui/types/types.js.map +1 -0
- package/es/chat-engine/core/a2ui/utils/binding.d.ts +34 -0
- package/es/chat-engine/core/a2ui/utils/binding.js +1784 -0
- package/es/chat-engine/core/a2ui/utils/binding.js.map +1 -0
- package/es/chat-engine/core/a2ui/utils/index.d.ts +5 -0
- package/es/chat-engine/core/a2ui/utils/index.js +13 -0
- package/es/chat-engine/core/a2ui/utils/index.js.map +1 -0
- package/es/chat-engine/core/a2ui/utils/validation.d.ts +70 -0
- package/es/chat-engine/core/a2ui/utils/validation.js +333 -0
- package/es/chat-engine/core/a2ui/utils/validation.js.map +1 -0
- package/es/chat-engine/core/adapters/agui/activity-manager.d.ts +102 -0
- package/es/chat-engine/core/adapters/agui/activity-manager.js +144 -0
- package/es/chat-engine/core/adapters/agui/activity-manager.js.map +1 -0
- package/es/chat-engine/core/adapters/agui/event-mapper.d.ts +149 -0
- package/es/chat-engine/core/adapters/agui/event-mapper.js +415 -0
- package/es/chat-engine/core/adapters/agui/event-mapper.js.map +1 -0
- package/es/chat-engine/core/adapters/agui/events.d.ts +1550 -0
- package/es/chat-engine/core/adapters/agui/events.js +199 -0
- package/es/chat-engine/core/adapters/agui/events.js.map +1 -0
- package/es/chat-engine/core/adapters/agui/index.d.ts +95 -0
- package/es/chat-engine/core/adapters/agui/index.js +195 -0
- package/es/chat-engine/core/adapters/agui/index.js.map +1 -0
- package/es/chat-engine/core/adapters/agui/state-manager.d.ts +99 -0
- package/es/chat-engine/core/adapters/agui/state-manager.js +168 -0
- package/es/chat-engine/core/adapters/agui/state-manager.js.map +1 -0
- package/es/chat-engine/core/adapters/agui/types.d.ts +818 -0
- package/es/chat-engine/core/adapters/agui/types.js +95 -0
- package/es/chat-engine/core/adapters/agui/types.js.map +1 -0
- package/es/chat-engine/core/adapters/agui/utils.d.ts +203 -0
- package/es/chat-engine/core/adapters/agui/utils.js +330 -0
- package/es/chat-engine/core/adapters/agui/utils.js.map +1 -0
- package/es/chat-engine/core/event-bus/ChatEventBus.d.ts +88 -0
- package/es/chat-engine/core/event-bus/ChatEventBus.js +295 -0
- package/es/chat-engine/core/event-bus/ChatEventBus.js.map +1 -0
- package/es/chat-engine/core/event-bus/index.d.ts +5 -0
- package/es/chat-engine/core/event-bus/index.js +13 -0
- package/es/chat-engine/core/event-bus/index.js.map +1 -0
- package/es/chat-engine/core/event-bus/types.d.ts +230 -0
- package/es/chat-engine/core/event-bus/types.js +32 -0
- package/es/chat-engine/core/event-bus/types.js.map +1 -0
- package/es/chat-engine/core/index.d.ts +147 -0
- package/es/chat-engine/core/index.js +754 -0
- package/es/chat-engine/core/index.js.map +1 -0
- package/es/chat-engine/core/processor/index.d.ts +20 -0
- package/es/chat-engine/core/processor/index.js +146 -0
- package/es/chat-engine/core/processor/index.js.map +1 -0
- package/es/chat-engine/core/server/batch-client.d.ts +20 -0
- package/es/chat-engine/core/server/batch-client.js +114 -0
- package/es/chat-engine/core/server/batch-client.js.map +1 -0
- package/es/chat-engine/core/server/connection-manager.d.ts +39 -0
- package/es/chat-engine/core/server/connection-manager.js +84 -0
- package/es/chat-engine/core/server/connection-manager.js.map +1 -0
- package/es/chat-engine/core/server/errors.d.ts +22 -0
- package/es/chat-engine/core/server/errors.js +80 -0
- package/es/chat-engine/core/server/errors.js.map +1 -0
- package/es/chat-engine/core/server/index.d.ts +11 -0
- package/es/chat-engine/core/server/index.js +26 -0
- package/es/chat-engine/core/server/index.js.map +1 -0
- package/es/chat-engine/core/server/llm-service.d.ts +44 -0
- package/es/chat-engine/core/server/llm-service.js +200 -0
- package/es/chat-engine/core/server/llm-service.js.map +1 -0
- package/es/chat-engine/core/server/sse-client.d.ts +77 -0
- package/es/chat-engine/core/server/sse-client.js +362 -0
- package/es/chat-engine/core/server/sse-client.js.map +1 -0
- package/es/chat-engine/core/server/sse-parser.d.ts +49 -0
- package/es/chat-engine/core/server/sse-parser.js +116 -0
- package/es/chat-engine/core/server/sse-parser.js.map +1 -0
- package/es/chat-engine/core/server/types.d.ts +54 -0
- package/es/chat-engine/core/server/types.js +28 -0
- package/es/chat-engine/core/server/types.js.map +1 -0
- package/es/chat-engine/core/store/message.d.ts +27 -0
- package/es/chat-engine/core/store/message.js +263 -0
- package/es/chat-engine/core/store/message.js.map +1 -0
- package/es/chat-engine/core/store/model.d.ts +8 -0
- package/es/chat-engine/core/store/model.js +65 -0
- package/es/chat-engine/core/store/model.js.map +1 -0
- package/es/chat-engine/core/store/reactiveState.d.ts +52 -0
- package/es/chat-engine/core/store/reactiveState.js +1359 -0
- package/es/chat-engine/core/store/reactiveState.js.map +1 -0
- package/es/chat-engine/core/type.d.ts +271 -0
- package/es/chat-engine/core/type.js +7 -0
- package/es/chat-engine/core/type.js.map +1 -0
- package/es/chat-engine/core/utils/eventEmitter.d.ts +10 -0
- package/es/chat-engine/core/utils/eventEmitter.js +67 -0
- package/es/chat-engine/core/utils/eventEmitter.js.map +1 -0
- package/es/chat-engine/core/utils/index.d.ts +33 -0
- package/es/chat-engine/core/utils/index.js +999 -0
- package/es/chat-engine/core/utils/index.js.map +1 -0
- package/es/chat-engine/core/utils/logger.d.ts +30 -0
- package/es/chat-engine/core/utils/logger.js +87 -0
- package/es/chat-engine/core/utils/logger.js.map +1 -0
- package/es/chat-engine/hooks/a2ui/A2UIContext.d.ts +90 -0
- package/es/chat-engine/hooks/a2ui/A2UIContext.js +97 -0
- package/es/chat-engine/hooks/a2ui/A2UIContext.js.map +1 -0
- package/es/chat-engine/hooks/a2ui/index.d.ts +12 -0
- package/es/chat-engine/hooks/a2ui/index.js +19 -0
- package/es/chat-engine/hooks/a2ui/useA2UIAction.d.ts +40 -0
- package/es/chat-engine/hooks/a2ui/useA2UIAction.js +74 -0
- package/es/chat-engine/hooks/a2ui/useA2UIAction.js.map +1 -0
- package/es/chat-engine/hooks/a2ui/useA2UISurface.d.ts +36 -0
- package/es/chat-engine/hooks/a2ui/useA2UISurface.js +39 -0
- package/es/chat-engine/hooks/a2ui/useA2UISurface.js.map +1 -0
- package/es/chat-engine/hooks/a2ui/useDataBinding.d.ts +53 -0
- package/es/chat-engine/hooks/a2ui/useDataBinding.js +111 -0
- package/es/chat-engine/hooks/a2ui/useDataBinding.js.map +1 -0
- package/es/chat-engine/hooks/index.d.ts +4 -0
- package/es/chat-engine/hooks/index.js +54 -0
- package/es/chat-engine/hooks/index.js.map +1 -0
- package/es/chat-engine/hooks/useAgentActivity.d.ts +15 -0
- package/es/chat-engine/hooks/useAgentActivity.js +73 -0
- package/es/chat-engine/hooks/useAgentActivity.js.map +1 -0
- package/es/chat-engine/hooks/useAgentState.js +39 -2
- package/es/chat-engine/hooks/useAgentState.js.map +1 -1
- package/es/chat-engine/hooks/useAgentToolcall.js +1 -1
- package/es/chat-engine/hooks/useChat.d.ts +3 -3
- package/es/chat-engine/hooks/useChat.js +41 -3
- package/es/chat-engine/hooks/useChat.js.map +1 -1
- package/es/chat-engine/hooks/useJsonRenderActivity.d.ts +56 -0
- package/es/chat-engine/hooks/useJsonRenderActivity.js +63 -0
- package/es/chat-engine/hooks/useJsonRenderActivity.js.map +1 -0
- package/es/chat-engine/index.d.ts +3 -3
- package/es/chat-engine/index.js +61 -5
- package/es/chat-engine/index.js.map +1 -1
- package/es/chat-filecard/_example-js/base.d.ts +1 -0
- package/es/chat-filecard/index.js +4 -6
- package/es/chat-filecard/index.js.map +1 -1
- package/es/chat-loading/_example-js/base.d.ts +2 -0
- package/es/chat-loading/_example-js/text.d.ts +2 -0
- package/es/chat-loading/index.js +4 -6
- package/es/chat-loading/index.js.map +1 -1
- package/es/chat-markdown/_example-js/base.d.ts +1 -0
- package/es/chat-markdown/_example-js/custom.d.ts +2 -0
- package/es/chat-markdown/_example-js/event.d.ts +1 -0
- package/es/chat-markdown/_example-js/footnote.d.ts +2 -0
- package/es/chat-markdown/_example-js/plugin.d.ts +2 -0
- package/es/chat-markdown/_example-js/theme.d.ts +2 -0
- package/es/chat-markdown/index.js +4 -6
- package/es/chat-markdown/index.js.map +1 -1
- package/es/chat-message/_example-js/action.d.ts +1 -0
- package/es/chat-message/_example-js/base.d.ts +1 -0
- package/es/chat-message/_example-js/configure.d.ts +1 -0
- package/es/chat-message/_example-js/content.d.ts +1 -0
- package/es/chat-message/_example-js/custom.d.ts +1 -0
- package/es/chat-message/_example-js/handle-actions.d.ts +7 -0
- package/es/chat-message/_example-js/status.d.ts +1 -0
- package/es/chat-message/_example-js/think.d.ts +1 -0
- package/es/chat-message/index.js +4 -6
- package/es/chat-message/index.js.map +1 -1
- package/es/chat-sender/_example-js/attachment.d.ts +2 -0
- package/es/chat-sender/_example-js/base.d.ts +2 -0
- package/es/chat-sender/_example-js/custom.d.ts +2 -0
- package/es/chat-sender/index.js +4 -6
- package/es/chat-sender/index.js.map +1 -1
- package/es/chat-thinking/_example-js/base.d.ts +1 -0
- package/es/chat-thinking/_example-js/style.d.ts +1 -0
- package/es/chat-thinking/index.js +4 -6
- package/es/chat-thinking/index.js.map +1 -1
- package/es/chatbot/_example-js/agent.d.ts +1 -0
- package/es/chatbot/_example-js/agui.d.ts +13 -0
- package/es/chatbot/_example-js/backup/travel.d.ts +1 -0
- package/es/chatbot/_example-js/basic.d.ts +1 -0
- package/es/chatbot/_example-js/code.d.ts +1 -0
- package/es/chatbot/_example-js/components/ItineraryCard.d.ts +3 -0
- package/es/chatbot/_example-js/components/login.d.ts +1 -0
- package/es/chatbot/_example-js/comprehensive.d.ts +1 -0
- package/es/chatbot/_example-js/custom-content.d.ts +1 -0
- package/es/chatbot/_example-js/custom-merge.d.ts +1 -0
- package/es/chatbot/_example-js/custom.d.ts +1 -0
- package/es/chatbot/_example-js/docs.d.ts +1 -0
- package/es/chatbot/_example-js/image.d.ts +1 -0
- package/es/chatbot/_example-js/initial-messages.d.ts +9 -0
- package/es/chatbot/_example-js/instance-methods.d.ts +14 -0
- package/es/chatbot/_example-js/nostream.d.ts +1 -0
- package/es/chatbot/_example-js/quick-start.d.ts +9 -0
- package/es/chatbot/_example-js/research.d.ts +1 -0
- package/es/chatbot/_example-js/role-message-config.d.ts +22 -0
- package/es/chatbot/_example-js/sender-config.d.ts +20 -0
- package/es/chatbot/_example-js/service-config.d.ts +17 -0
- package/es/chatbot/_example-js/simple.d.ts +1 -0
- package/es/chatbot/_example-js/utils/messageRenderer.d.ts +5 -0
- package/es/chatbot/docs/react-best-practice.d.ts +13 -0
- package/es/chatbot/index.js +4 -6
- package/es/chatbot/index.js.map +1 -1
- package/es/index.js +59 -9
- package/es/index.js.map +1 -1
- package/es/style/index.js +1 -1
- package/package.json +6 -3
- package/es/chatbot/components/provider/agent-state.d.ts +0 -5
- package/es/chatbot/components/provider/agent-state.js +0 -28
- package/es/chatbot/components/provider/agent-state.js.map +0 -1
- package/es/chatbot/components/toolcall/index.js +0 -19
- package/es/chatbot/components/toolcall/registry.d.ts +0 -35
- package/es/chatbot/components/toolcall/registry.js.map +0 -1
- package/es/chatbot/components/toolcall/render.d.ts +0 -13
- package/es/chatbot/components/toolcall/render.js +0 -207
- package/es/chatbot/components/toolcall/render.js.map +0 -1
- package/es/chatbot/components/toolcall/types.d.ts +0 -57
- package/es/chatbot/components/toolcall/types.js +0 -15
- package/es/chatbot/components/toolcall/types.js.map +0 -1
- package/es/chatbot/hooks/useAgentState.d.ts +0 -40
- package/es/chatbot/hooks/useAgentState.js +0 -76
- package/es/chatbot/hooks/useAgentState.js.map +0 -1
- package/es/chatbot/hooks/useAgentToolcall.d.ts +0 -28
- package/es/chatbot/hooks/useAgentToolcall.js +0 -78
- package/es/chatbot/hooks/useAgentToolcall.js.map +0 -1
- package/es/chatbot/hooks/useChat.d.ts +0 -11
- package/es/chatbot/hooks/useChat.js +0 -66
- package/es/chatbot/hooks/useChat.js.map +0 -1
- /package/es/{chatbot/components/toolcall → chat-engine/hooks/a2ui}/index.js.map +0 -0
|
@@ -0,0 +1,1784 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tdesign v1.0.2-alpha.14
|
|
3
|
+
* (c) 2026 tdesign
|
|
4
|
+
* @license MIT
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
|
|
8
|
+
import _typeof from '@babel/runtime/helpers/typeof';
|
|
9
|
+
|
|
10
|
+
var INUMBER = 'INUMBER';
|
|
11
|
+
var IOP1 = 'IOP1';
|
|
12
|
+
var IOP2 = 'IOP2';
|
|
13
|
+
var IOP3 = 'IOP3';
|
|
14
|
+
var IVAR = 'IVAR';
|
|
15
|
+
var IVARNAME = 'IVARNAME';
|
|
16
|
+
var IFUNCALL = 'IFUNCALL';
|
|
17
|
+
var IFUNDEF = 'IFUNDEF';
|
|
18
|
+
var IEXPR = 'IEXPR';
|
|
19
|
+
var IEXPREVAL = 'IEXPREVAL';
|
|
20
|
+
var IMEMBER = 'IMEMBER';
|
|
21
|
+
var IENDSTATEMENT = 'IENDSTATEMENT';
|
|
22
|
+
var IARRAY = 'IARRAY';
|
|
23
|
+
function Instruction(type, value) {
|
|
24
|
+
this.type = type;
|
|
25
|
+
this.value = value !== undefined && value !== null ? value : 0;
|
|
26
|
+
}
|
|
27
|
+
Instruction.prototype.toString = function () {
|
|
28
|
+
switch (this.type) {
|
|
29
|
+
case INUMBER:
|
|
30
|
+
case IOP1:
|
|
31
|
+
case IOP2:
|
|
32
|
+
case IOP3:
|
|
33
|
+
case IVAR:
|
|
34
|
+
case IVARNAME:
|
|
35
|
+
case IENDSTATEMENT:
|
|
36
|
+
return this.value;
|
|
37
|
+
case IFUNCALL:
|
|
38
|
+
return 'CALL ' + this.value;
|
|
39
|
+
case IFUNDEF:
|
|
40
|
+
return 'DEF ' + this.value;
|
|
41
|
+
case IARRAY:
|
|
42
|
+
return 'ARRAY ' + this.value;
|
|
43
|
+
case IMEMBER:
|
|
44
|
+
return '.' + this.value;
|
|
45
|
+
default:
|
|
46
|
+
return 'Invalid Instruction';
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
function unaryInstruction(value) {
|
|
50
|
+
return new Instruction(IOP1, value);
|
|
51
|
+
}
|
|
52
|
+
function binaryInstruction(value) {
|
|
53
|
+
return new Instruction(IOP2, value);
|
|
54
|
+
}
|
|
55
|
+
function ternaryInstruction(value) {
|
|
56
|
+
return new Instruction(IOP3, value);
|
|
57
|
+
}
|
|
58
|
+
function simplify(tokens, unaryOps, binaryOps, ternaryOps, values) {
|
|
59
|
+
var nstack = [];
|
|
60
|
+
var newexpression = [];
|
|
61
|
+
var n1, n2, n3;
|
|
62
|
+
var f;
|
|
63
|
+
for (var i = 0; i < tokens.length; i++) {
|
|
64
|
+
var item = tokens[i];
|
|
65
|
+
var type = item.type;
|
|
66
|
+
if (type === INUMBER || type === IVARNAME) {
|
|
67
|
+
if (Array.isArray(item.value)) {
|
|
68
|
+
nstack.push.apply(nstack, simplify(item.value.map(function (x) {
|
|
69
|
+
return new Instruction(INUMBER, x);
|
|
70
|
+
}).concat(new Instruction(IARRAY, item.value.length)), unaryOps, binaryOps, ternaryOps, values));
|
|
71
|
+
} else {
|
|
72
|
+
nstack.push(item);
|
|
73
|
+
}
|
|
74
|
+
} else if (type === IVAR && values.hasOwnProperty(item.value)) {
|
|
75
|
+
item = new Instruction(INUMBER, values[item.value]);
|
|
76
|
+
nstack.push(item);
|
|
77
|
+
} else if (type === IOP2 && nstack.length > 1) {
|
|
78
|
+
n2 = nstack.pop();
|
|
79
|
+
n1 = nstack.pop();
|
|
80
|
+
f = binaryOps[item.value];
|
|
81
|
+
item = new Instruction(INUMBER, f(n1.value, n2.value));
|
|
82
|
+
nstack.push(item);
|
|
83
|
+
} else if (type === IOP3 && nstack.length > 2) {
|
|
84
|
+
n3 = nstack.pop();
|
|
85
|
+
n2 = nstack.pop();
|
|
86
|
+
n1 = nstack.pop();
|
|
87
|
+
if (item.value === '?') {
|
|
88
|
+
nstack.push(n1.value ? n2.value : n3.value);
|
|
89
|
+
} else {
|
|
90
|
+
f = ternaryOps[item.value];
|
|
91
|
+
item = new Instruction(INUMBER, f(n1.value, n2.value, n3.value));
|
|
92
|
+
nstack.push(item);
|
|
93
|
+
}
|
|
94
|
+
} else if (type === IOP1 && nstack.length > 0) {
|
|
95
|
+
n1 = nstack.pop();
|
|
96
|
+
f = unaryOps[item.value];
|
|
97
|
+
item = new Instruction(INUMBER, f(n1.value));
|
|
98
|
+
nstack.push(item);
|
|
99
|
+
} else if (type === IEXPR) {
|
|
100
|
+
while (nstack.length > 0) {
|
|
101
|
+
newexpression.push(nstack.shift());
|
|
102
|
+
}
|
|
103
|
+
newexpression.push(new Instruction(IEXPR, simplify(item.value, unaryOps, binaryOps, ternaryOps, values)));
|
|
104
|
+
} else if (type === IMEMBER && nstack.length > 0) {
|
|
105
|
+
n1 = nstack.pop();
|
|
106
|
+
nstack.push(new Instruction(INUMBER, n1.value[item.value]));
|
|
107
|
+
} /* else if (type === IARRAY && nstack.length >= item.value) {
|
|
108
|
+
var length = item.value;
|
|
109
|
+
while (length-- > 0) {
|
|
110
|
+
newexpression.push(nstack.pop());
|
|
111
|
+
}
|
|
112
|
+
newexpression.push(new Instruction(IARRAY, item.value));
|
|
113
|
+
} */else {
|
|
114
|
+
while (nstack.length > 0) {
|
|
115
|
+
newexpression.push(nstack.shift());
|
|
116
|
+
}
|
|
117
|
+
newexpression.push(item);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
while (nstack.length > 0) {
|
|
121
|
+
newexpression.push(nstack.shift());
|
|
122
|
+
}
|
|
123
|
+
return newexpression;
|
|
124
|
+
}
|
|
125
|
+
function substitute(tokens, variable, expr) {
|
|
126
|
+
var newexpression = [];
|
|
127
|
+
for (var i = 0; i < tokens.length; i++) {
|
|
128
|
+
var item = tokens[i];
|
|
129
|
+
var type = item.type;
|
|
130
|
+
if (type === IVAR && item.value === variable) {
|
|
131
|
+
for (var j = 0; j < expr.tokens.length; j++) {
|
|
132
|
+
var expritem = expr.tokens[j];
|
|
133
|
+
var replitem;
|
|
134
|
+
if (expritem.type === IOP1) {
|
|
135
|
+
replitem = unaryInstruction(expritem.value);
|
|
136
|
+
} else if (expritem.type === IOP2) {
|
|
137
|
+
replitem = binaryInstruction(expritem.value);
|
|
138
|
+
} else if (expritem.type === IOP3) {
|
|
139
|
+
replitem = ternaryInstruction(expritem.value);
|
|
140
|
+
} else {
|
|
141
|
+
replitem = new Instruction(expritem.type, expritem.value);
|
|
142
|
+
}
|
|
143
|
+
newexpression.push(replitem);
|
|
144
|
+
}
|
|
145
|
+
} else if (type === IEXPR) {
|
|
146
|
+
newexpression.push(new Instruction(IEXPR, substitute(item.value, variable, expr)));
|
|
147
|
+
} else {
|
|
148
|
+
newexpression.push(item);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return newexpression;
|
|
152
|
+
}
|
|
153
|
+
function evaluate(tokens, expr, values) {
|
|
154
|
+
var nstack = [];
|
|
155
|
+
var n1, n2, n3;
|
|
156
|
+
var f, args, argCount;
|
|
157
|
+
if (isExpressionEvaluator(tokens)) {
|
|
158
|
+
return resolveExpression(tokens, values);
|
|
159
|
+
}
|
|
160
|
+
var numTokens = tokens.length;
|
|
161
|
+
for (var i = 0; i < numTokens; i++) {
|
|
162
|
+
var item = tokens[i];
|
|
163
|
+
var type = item.type;
|
|
164
|
+
if (type === INUMBER || type === IVARNAME) {
|
|
165
|
+
nstack.push(item.value);
|
|
166
|
+
} else if (type === IOP2) {
|
|
167
|
+
n2 = nstack.pop();
|
|
168
|
+
n1 = nstack.pop();
|
|
169
|
+
if (item.value === 'and') {
|
|
170
|
+
nstack.push(n1 ? !!evaluate(n2, expr, values) : false);
|
|
171
|
+
} else if (item.value === 'or') {
|
|
172
|
+
nstack.push(n1 ? true : !!evaluate(n2, expr, values));
|
|
173
|
+
} else if (item.value === '=') {
|
|
174
|
+
f = expr.binaryOps[item.value];
|
|
175
|
+
nstack.push(f(n1, evaluate(n2, expr, values), values));
|
|
176
|
+
} else {
|
|
177
|
+
f = expr.binaryOps[item.value];
|
|
178
|
+
nstack.push(f(resolveExpression(n1, values), resolveExpression(n2, values)));
|
|
179
|
+
}
|
|
180
|
+
} else if (type === IOP3) {
|
|
181
|
+
n3 = nstack.pop();
|
|
182
|
+
n2 = nstack.pop();
|
|
183
|
+
n1 = nstack.pop();
|
|
184
|
+
if (item.value === '?') {
|
|
185
|
+
nstack.push(evaluate(n1 ? n2 : n3, expr, values));
|
|
186
|
+
} else {
|
|
187
|
+
f = expr.ternaryOps[item.value];
|
|
188
|
+
nstack.push(f(resolveExpression(n1, values), resolveExpression(n2, values), resolveExpression(n3, values)));
|
|
189
|
+
}
|
|
190
|
+
} else if (type === IVAR) {
|
|
191
|
+
if (item.value in expr.functions) {
|
|
192
|
+
nstack.push(expr.functions[item.value]);
|
|
193
|
+
} else if (item.value in expr.unaryOps && expr.parser.isOperatorEnabled(item.value)) {
|
|
194
|
+
nstack.push(expr.unaryOps[item.value]);
|
|
195
|
+
} else {
|
|
196
|
+
var v = values[item.value];
|
|
197
|
+
if (v !== undefined) {
|
|
198
|
+
nstack.push(v);
|
|
199
|
+
} else {
|
|
200
|
+
throw new Error('undefined variable: ' + item.value);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
} else if (type === IOP1) {
|
|
204
|
+
n1 = nstack.pop();
|
|
205
|
+
f = expr.unaryOps[item.value];
|
|
206
|
+
nstack.push(f(resolveExpression(n1, values)));
|
|
207
|
+
} else if (type === IFUNCALL) {
|
|
208
|
+
argCount = item.value;
|
|
209
|
+
args = [];
|
|
210
|
+
while (argCount-- > 0) {
|
|
211
|
+
args.unshift(resolveExpression(nstack.pop(), values));
|
|
212
|
+
}
|
|
213
|
+
f = nstack.pop();
|
|
214
|
+
if (f.apply && f.call) {
|
|
215
|
+
nstack.push(f.apply(undefined, args));
|
|
216
|
+
} else {
|
|
217
|
+
throw new Error(f + ' is not a function');
|
|
218
|
+
}
|
|
219
|
+
} else if (type === IFUNDEF) {
|
|
220
|
+
// Create closure to keep references to arguments and expression
|
|
221
|
+
nstack.push(function () {
|
|
222
|
+
var n2 = nstack.pop();
|
|
223
|
+
var args = [];
|
|
224
|
+
var argCount = item.value;
|
|
225
|
+
while (argCount-- > 0) {
|
|
226
|
+
args.unshift(nstack.pop());
|
|
227
|
+
}
|
|
228
|
+
var n1 = nstack.pop();
|
|
229
|
+
var f = function f() {
|
|
230
|
+
var scope = Object.assign({}, values);
|
|
231
|
+
for (var i = 0, len = args.length; i < len; i++) {
|
|
232
|
+
scope[args[i]] = arguments[i];
|
|
233
|
+
}
|
|
234
|
+
return evaluate(n2, expr, scope);
|
|
235
|
+
};
|
|
236
|
+
// f.name = n1
|
|
237
|
+
Object.defineProperty(f, 'name', {
|
|
238
|
+
value: n1,
|
|
239
|
+
writable: false
|
|
240
|
+
});
|
|
241
|
+
values[n1] = f;
|
|
242
|
+
return f;
|
|
243
|
+
}());
|
|
244
|
+
} else if (type === IEXPR) {
|
|
245
|
+
nstack.push(createExpressionEvaluator(item, expr));
|
|
246
|
+
} else if (type === IEXPREVAL) {
|
|
247
|
+
nstack.push(item);
|
|
248
|
+
} else if (type === IMEMBER) {
|
|
249
|
+
n1 = nstack.pop();
|
|
250
|
+
nstack.push(n1[item.value]);
|
|
251
|
+
} else if (type === IENDSTATEMENT) {
|
|
252
|
+
nstack.pop();
|
|
253
|
+
} else if (type === IARRAY) {
|
|
254
|
+
argCount = item.value;
|
|
255
|
+
args = [];
|
|
256
|
+
while (argCount-- > 0) {
|
|
257
|
+
args.unshift(nstack.pop());
|
|
258
|
+
}
|
|
259
|
+
nstack.push(args);
|
|
260
|
+
} else {
|
|
261
|
+
throw new Error('invalid Expression');
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
if (nstack.length > 1) {
|
|
265
|
+
throw new Error('invalid Expression (parity)');
|
|
266
|
+
}
|
|
267
|
+
// Explicitly return zero to avoid test issues caused by -0
|
|
268
|
+
return nstack[0] === 0 ? 0 : resolveExpression(nstack[0], values);
|
|
269
|
+
}
|
|
270
|
+
function createExpressionEvaluator(token, expr, values) {
|
|
271
|
+
if (isExpressionEvaluator(token)) return token;
|
|
272
|
+
return {
|
|
273
|
+
type: IEXPREVAL,
|
|
274
|
+
value: function value(scope) {
|
|
275
|
+
return evaluate(token.value, expr, scope);
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
function isExpressionEvaluator(n) {
|
|
280
|
+
return n && n.type === IEXPREVAL;
|
|
281
|
+
}
|
|
282
|
+
function resolveExpression(n, values) {
|
|
283
|
+
return isExpressionEvaluator(n) ? n.value(values) : n;
|
|
284
|
+
}
|
|
285
|
+
function expressionToString(tokens, toJS) {
|
|
286
|
+
var nstack = [];
|
|
287
|
+
var n1, n2, n3;
|
|
288
|
+
var f, args, argCount;
|
|
289
|
+
for (var i = 0; i < tokens.length; i++) {
|
|
290
|
+
var item = tokens[i];
|
|
291
|
+
var type = item.type;
|
|
292
|
+
if (type === INUMBER) {
|
|
293
|
+
if (typeof item.value === 'number' && item.value < 0) {
|
|
294
|
+
nstack.push('(' + item.value + ')');
|
|
295
|
+
} else if (Array.isArray(item.value)) {
|
|
296
|
+
nstack.push('[' + item.value.map(escapeValue).join(', ') + ']');
|
|
297
|
+
} else {
|
|
298
|
+
nstack.push(escapeValue(item.value));
|
|
299
|
+
}
|
|
300
|
+
} else if (type === IOP2) {
|
|
301
|
+
n2 = nstack.pop();
|
|
302
|
+
n1 = nstack.pop();
|
|
303
|
+
f = item.value;
|
|
304
|
+
if (toJS) {
|
|
305
|
+
if (f === '^') {
|
|
306
|
+
nstack.push('Math.pow(' + n1 + ', ' + n2 + ')');
|
|
307
|
+
} else if (f === 'and') {
|
|
308
|
+
nstack.push('(!!' + n1 + ' && !!' + n2 + ')');
|
|
309
|
+
} else if (f === 'or') {
|
|
310
|
+
nstack.push('(!!' + n1 + ' || !!' + n2 + ')');
|
|
311
|
+
} else if (f === '||') {
|
|
312
|
+
nstack.push('(function(a,b){ return Array.isArray(a) && Array.isArray(b) ? a.concat(b) : String(a) + String(b); }((' + n1 + '),(' + n2 + ')))');
|
|
313
|
+
} else if (f === '==') {
|
|
314
|
+
nstack.push('(' + n1 + ' === ' + n2 + ')');
|
|
315
|
+
} else if (f === '!=') {
|
|
316
|
+
nstack.push('(' + n1 + ' !== ' + n2 + ')');
|
|
317
|
+
} else if (f === '[') {
|
|
318
|
+
nstack.push(n1 + '[(' + n2 + ') | 0]');
|
|
319
|
+
} else {
|
|
320
|
+
nstack.push('(' + n1 + ' ' + f + ' ' + n2 + ')');
|
|
321
|
+
}
|
|
322
|
+
} else {
|
|
323
|
+
if (f === '[') {
|
|
324
|
+
nstack.push(n1 + '[' + n2 + ']');
|
|
325
|
+
} else {
|
|
326
|
+
nstack.push('(' + n1 + ' ' + f + ' ' + n2 + ')');
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
} else if (type === IOP3) {
|
|
330
|
+
n3 = nstack.pop();
|
|
331
|
+
n2 = nstack.pop();
|
|
332
|
+
n1 = nstack.pop();
|
|
333
|
+
f = item.value;
|
|
334
|
+
if (f === '?') {
|
|
335
|
+
nstack.push('(' + n1 + ' ? ' + n2 + ' : ' + n3 + ')');
|
|
336
|
+
} else {
|
|
337
|
+
throw new Error('invalid Expression');
|
|
338
|
+
}
|
|
339
|
+
} else if (type === IVAR || type === IVARNAME) {
|
|
340
|
+
nstack.push(item.value);
|
|
341
|
+
} else if (type === IOP1) {
|
|
342
|
+
n1 = nstack.pop();
|
|
343
|
+
f = item.value;
|
|
344
|
+
if (f === '-' || f === '+') {
|
|
345
|
+
nstack.push('(' + f + n1 + ')');
|
|
346
|
+
} else if (toJS) {
|
|
347
|
+
if (f === 'not') {
|
|
348
|
+
nstack.push('(' + '!' + n1 + ')');
|
|
349
|
+
} else if (f === '!') {
|
|
350
|
+
nstack.push('fac(' + n1 + ')');
|
|
351
|
+
} else {
|
|
352
|
+
nstack.push(f + '(' + n1 + ')');
|
|
353
|
+
}
|
|
354
|
+
} else if (f === '!') {
|
|
355
|
+
nstack.push('(' + n1 + '!)');
|
|
356
|
+
} else {
|
|
357
|
+
nstack.push('(' + f + ' ' + n1 + ')');
|
|
358
|
+
}
|
|
359
|
+
} else if (type === IFUNCALL) {
|
|
360
|
+
argCount = item.value;
|
|
361
|
+
args = [];
|
|
362
|
+
while (argCount-- > 0) {
|
|
363
|
+
args.unshift(nstack.pop());
|
|
364
|
+
}
|
|
365
|
+
f = nstack.pop();
|
|
366
|
+
nstack.push(f + '(' + args.join(', ') + ')');
|
|
367
|
+
} else if (type === IFUNDEF) {
|
|
368
|
+
n2 = nstack.pop();
|
|
369
|
+
argCount = item.value;
|
|
370
|
+
args = [];
|
|
371
|
+
while (argCount-- > 0) {
|
|
372
|
+
args.unshift(nstack.pop());
|
|
373
|
+
}
|
|
374
|
+
n1 = nstack.pop();
|
|
375
|
+
if (toJS) {
|
|
376
|
+
nstack.push('(' + n1 + ' = function(' + args.join(', ') + ') { return ' + n2 + ' })');
|
|
377
|
+
} else {
|
|
378
|
+
nstack.push('(' + n1 + '(' + args.join(', ') + ') = ' + n2 + ')');
|
|
379
|
+
}
|
|
380
|
+
} else if (type === IMEMBER) {
|
|
381
|
+
n1 = nstack.pop();
|
|
382
|
+
nstack.push(n1 + '.' + item.value);
|
|
383
|
+
} else if (type === IARRAY) {
|
|
384
|
+
argCount = item.value;
|
|
385
|
+
args = [];
|
|
386
|
+
while (argCount-- > 0) {
|
|
387
|
+
args.unshift(nstack.pop());
|
|
388
|
+
}
|
|
389
|
+
nstack.push('[' + args.join(', ') + ']');
|
|
390
|
+
} else if (type === IEXPR) {
|
|
391
|
+
nstack.push('(' + expressionToString(item.value, toJS) + ')');
|
|
392
|
+
} else if (type === IENDSTATEMENT) ;else {
|
|
393
|
+
throw new Error('invalid Expression');
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
if (nstack.length > 1) {
|
|
397
|
+
if (toJS) {
|
|
398
|
+
nstack = [nstack.join(',')];
|
|
399
|
+
} else {
|
|
400
|
+
nstack = [nstack.join(';')];
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
return String(nstack[0]);
|
|
404
|
+
}
|
|
405
|
+
function escapeValue(v) {
|
|
406
|
+
if (typeof v === 'string') {
|
|
407
|
+
return JSON.stringify(v).replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029");
|
|
408
|
+
}
|
|
409
|
+
return v;
|
|
410
|
+
}
|
|
411
|
+
function contains(array, obj) {
|
|
412
|
+
for (var i = 0; i < array.length; i++) {
|
|
413
|
+
if (array[i] === obj) {
|
|
414
|
+
return true;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
return false;
|
|
418
|
+
}
|
|
419
|
+
function getSymbols(tokens, symbols, options) {
|
|
420
|
+
options = options || {};
|
|
421
|
+
var withMembers = !!options.withMembers;
|
|
422
|
+
var prevVar = null;
|
|
423
|
+
for (var i = 0; i < tokens.length; i++) {
|
|
424
|
+
var item = tokens[i];
|
|
425
|
+
if (item.type === IVAR || item.type === IVARNAME) {
|
|
426
|
+
if (!withMembers && !contains(symbols, item.value)) {
|
|
427
|
+
symbols.push(item.value);
|
|
428
|
+
} else if (prevVar !== null) {
|
|
429
|
+
if (!contains(symbols, prevVar)) {
|
|
430
|
+
symbols.push(prevVar);
|
|
431
|
+
}
|
|
432
|
+
prevVar = item.value;
|
|
433
|
+
} else {
|
|
434
|
+
prevVar = item.value;
|
|
435
|
+
}
|
|
436
|
+
} else if (item.type === IMEMBER && withMembers && prevVar !== null) {
|
|
437
|
+
prevVar += '.' + item.value;
|
|
438
|
+
} else if (item.type === IEXPR) {
|
|
439
|
+
getSymbols(item.value, symbols, options);
|
|
440
|
+
} else if (prevVar !== null) {
|
|
441
|
+
if (!contains(symbols, prevVar)) {
|
|
442
|
+
symbols.push(prevVar);
|
|
443
|
+
}
|
|
444
|
+
prevVar = null;
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
if (prevVar !== null && !contains(symbols, prevVar)) {
|
|
448
|
+
symbols.push(prevVar);
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
function Expression(tokens, parser) {
|
|
452
|
+
this.tokens = tokens;
|
|
453
|
+
this.parser = parser;
|
|
454
|
+
this.unaryOps = parser.unaryOps;
|
|
455
|
+
this.binaryOps = parser.binaryOps;
|
|
456
|
+
this.ternaryOps = parser.ternaryOps;
|
|
457
|
+
this.functions = parser.functions;
|
|
458
|
+
}
|
|
459
|
+
Expression.prototype.simplify = function (values) {
|
|
460
|
+
values = values || {};
|
|
461
|
+
return new Expression(simplify(this.tokens, this.unaryOps, this.binaryOps, this.ternaryOps, values), this.parser);
|
|
462
|
+
};
|
|
463
|
+
Expression.prototype.substitute = function (variable, expr) {
|
|
464
|
+
if (!(expr instanceof Expression)) {
|
|
465
|
+
expr = this.parser.parse(String(expr));
|
|
466
|
+
}
|
|
467
|
+
return new Expression(substitute(this.tokens, variable, expr), this.parser);
|
|
468
|
+
};
|
|
469
|
+
Expression.prototype.evaluate = function (values) {
|
|
470
|
+
values = values || {};
|
|
471
|
+
return evaluate(this.tokens, this, values);
|
|
472
|
+
};
|
|
473
|
+
Expression.prototype.toString = function () {
|
|
474
|
+
return expressionToString(this.tokens, false);
|
|
475
|
+
};
|
|
476
|
+
Expression.prototype.symbols = function (options) {
|
|
477
|
+
options = options || {};
|
|
478
|
+
var vars = [];
|
|
479
|
+
getSymbols(this.tokens, vars, options);
|
|
480
|
+
return vars;
|
|
481
|
+
};
|
|
482
|
+
Expression.prototype.variables = function (options) {
|
|
483
|
+
options = options || {};
|
|
484
|
+
var vars = [];
|
|
485
|
+
getSymbols(this.tokens, vars, options);
|
|
486
|
+
var functions = this.functions;
|
|
487
|
+
return vars.filter(function (name) {
|
|
488
|
+
return !(name in functions);
|
|
489
|
+
});
|
|
490
|
+
};
|
|
491
|
+
Expression.prototype.toJSFunction = function (param, variables) {
|
|
492
|
+
var expr = this;
|
|
493
|
+
var f = new Function(param, 'with(this.functions) with (this.ternaryOps) with (this.binaryOps) with (this.unaryOps) { return ' + expressionToString(this.simplify(variables).tokens, true) + '; }'); // eslint-disable-line no-new-func
|
|
494
|
+
return function () {
|
|
495
|
+
return f.apply(expr, arguments);
|
|
496
|
+
};
|
|
497
|
+
};
|
|
498
|
+
var TEOF = 'TEOF';
|
|
499
|
+
var TOP = 'TOP';
|
|
500
|
+
var TNUMBER = 'TNUMBER';
|
|
501
|
+
var TSTRING = 'TSTRING';
|
|
502
|
+
var TPAREN = 'TPAREN';
|
|
503
|
+
var TBRACKET = 'TBRACKET';
|
|
504
|
+
var TCOMMA = 'TCOMMA';
|
|
505
|
+
var TNAME = 'TNAME';
|
|
506
|
+
var TSEMICOLON = 'TSEMICOLON';
|
|
507
|
+
function Token(type, value, index) {
|
|
508
|
+
this.type = type;
|
|
509
|
+
this.value = value;
|
|
510
|
+
this.index = index;
|
|
511
|
+
}
|
|
512
|
+
Token.prototype.toString = function () {
|
|
513
|
+
return this.type + ': ' + this.value;
|
|
514
|
+
};
|
|
515
|
+
function TokenStream(parser, expression) {
|
|
516
|
+
this.pos = 0;
|
|
517
|
+
this.current = null;
|
|
518
|
+
this.unaryOps = parser.unaryOps;
|
|
519
|
+
this.binaryOps = parser.binaryOps;
|
|
520
|
+
this.ternaryOps = parser.ternaryOps;
|
|
521
|
+
this.consts = parser.consts;
|
|
522
|
+
this.expression = expression;
|
|
523
|
+
this.savedPosition = 0;
|
|
524
|
+
this.savedCurrent = null;
|
|
525
|
+
this.options = parser.options;
|
|
526
|
+
this.parser = parser;
|
|
527
|
+
}
|
|
528
|
+
TokenStream.prototype.newToken = function (type, value, pos) {
|
|
529
|
+
return new Token(type, value, pos != null ? pos : this.pos);
|
|
530
|
+
};
|
|
531
|
+
TokenStream.prototype.save = function () {
|
|
532
|
+
this.savedPosition = this.pos;
|
|
533
|
+
this.savedCurrent = this.current;
|
|
534
|
+
};
|
|
535
|
+
TokenStream.prototype.restore = function () {
|
|
536
|
+
this.pos = this.savedPosition;
|
|
537
|
+
this.current = this.savedCurrent;
|
|
538
|
+
};
|
|
539
|
+
TokenStream.prototype.next = function () {
|
|
540
|
+
if (this.pos >= this.expression.length) {
|
|
541
|
+
return this.newToken(TEOF, 'EOF');
|
|
542
|
+
}
|
|
543
|
+
if (this.isWhitespace() || this.isComment()) {
|
|
544
|
+
return this.next();
|
|
545
|
+
} else if (this.isRadixInteger() || this.isNumber() || this.isOperator() || this.isString() || this.isParen() || this.isBracket() || this.isComma() || this.isSemicolon() || this.isNamedOp() || this.isConst() || this.isName()) {
|
|
546
|
+
return this.current;
|
|
547
|
+
} else {
|
|
548
|
+
this.parseError('Unknown character "' + this.expression.charAt(this.pos) + '"');
|
|
549
|
+
}
|
|
550
|
+
};
|
|
551
|
+
TokenStream.prototype.isString = function () {
|
|
552
|
+
var r = false;
|
|
553
|
+
var startPos = this.pos;
|
|
554
|
+
var quote = this.expression.charAt(startPos);
|
|
555
|
+
if (quote === '\'' || quote === '"') {
|
|
556
|
+
var index = this.expression.indexOf(quote, startPos + 1);
|
|
557
|
+
while (index >= 0 && this.pos < this.expression.length) {
|
|
558
|
+
this.pos = index + 1;
|
|
559
|
+
if (this.expression.charAt(index - 1) !== '\\') {
|
|
560
|
+
var rawString = this.expression.substring(startPos + 1, index);
|
|
561
|
+
this.current = this.newToken(TSTRING, this.unescape(rawString), startPos);
|
|
562
|
+
r = true;
|
|
563
|
+
break;
|
|
564
|
+
}
|
|
565
|
+
index = this.expression.indexOf(quote, index + 1);
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
return r;
|
|
569
|
+
};
|
|
570
|
+
TokenStream.prototype.isParen = function () {
|
|
571
|
+
var c = this.expression.charAt(this.pos);
|
|
572
|
+
if (c === '(' || c === ')') {
|
|
573
|
+
this.current = this.newToken(TPAREN, c);
|
|
574
|
+
this.pos++;
|
|
575
|
+
return true;
|
|
576
|
+
}
|
|
577
|
+
return false;
|
|
578
|
+
};
|
|
579
|
+
TokenStream.prototype.isBracket = function () {
|
|
580
|
+
var c = this.expression.charAt(this.pos);
|
|
581
|
+
if ((c === '[' || c === ']') && this.isOperatorEnabled('[')) {
|
|
582
|
+
this.current = this.newToken(TBRACKET, c);
|
|
583
|
+
this.pos++;
|
|
584
|
+
return true;
|
|
585
|
+
}
|
|
586
|
+
return false;
|
|
587
|
+
};
|
|
588
|
+
TokenStream.prototype.isComma = function () {
|
|
589
|
+
var c = this.expression.charAt(this.pos);
|
|
590
|
+
if (c === ',') {
|
|
591
|
+
this.current = this.newToken(TCOMMA, ',');
|
|
592
|
+
this.pos++;
|
|
593
|
+
return true;
|
|
594
|
+
}
|
|
595
|
+
return false;
|
|
596
|
+
};
|
|
597
|
+
TokenStream.prototype.isSemicolon = function () {
|
|
598
|
+
var c = this.expression.charAt(this.pos);
|
|
599
|
+
if (c === ';') {
|
|
600
|
+
this.current = this.newToken(TSEMICOLON, ';');
|
|
601
|
+
this.pos++;
|
|
602
|
+
return true;
|
|
603
|
+
}
|
|
604
|
+
return false;
|
|
605
|
+
};
|
|
606
|
+
TokenStream.prototype.isConst = function () {
|
|
607
|
+
var startPos = this.pos;
|
|
608
|
+
var i = startPos;
|
|
609
|
+
for (; i < this.expression.length; i++) {
|
|
610
|
+
var c = this.expression.charAt(i);
|
|
611
|
+
if (c.toUpperCase() === c.toLowerCase()) {
|
|
612
|
+
if (i === this.pos || c !== '_' && c !== '.' && (c < '0' || c > '9')) {
|
|
613
|
+
break;
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
if (i > startPos) {
|
|
618
|
+
var str = this.expression.substring(startPos, i);
|
|
619
|
+
if (str in this.consts) {
|
|
620
|
+
this.current = this.newToken(TNUMBER, this.consts[str]);
|
|
621
|
+
this.pos += str.length;
|
|
622
|
+
return true;
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
return false;
|
|
626
|
+
};
|
|
627
|
+
TokenStream.prototype.isNamedOp = function () {
|
|
628
|
+
var startPos = this.pos;
|
|
629
|
+
var i = startPos;
|
|
630
|
+
for (; i < this.expression.length; i++) {
|
|
631
|
+
var c = this.expression.charAt(i);
|
|
632
|
+
if (c.toUpperCase() === c.toLowerCase()) {
|
|
633
|
+
if (i === this.pos || c !== '_' && (c < '0' || c > '9')) {
|
|
634
|
+
break;
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
if (i > startPos) {
|
|
639
|
+
var str = this.expression.substring(startPos, i);
|
|
640
|
+
if (this.isOperatorEnabled(str) && (str in this.binaryOps || str in this.unaryOps || str in this.ternaryOps)) {
|
|
641
|
+
this.current = this.newToken(TOP, str);
|
|
642
|
+
this.pos += str.length;
|
|
643
|
+
return true;
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
return false;
|
|
647
|
+
};
|
|
648
|
+
TokenStream.prototype.isName = function () {
|
|
649
|
+
var startPos = this.pos;
|
|
650
|
+
var i = startPos;
|
|
651
|
+
var hasLetter = false;
|
|
652
|
+
for (; i < this.expression.length; i++) {
|
|
653
|
+
var c = this.expression.charAt(i);
|
|
654
|
+
if (c.toUpperCase() === c.toLowerCase()) {
|
|
655
|
+
if (i === this.pos && (c === '$' || c === '_')) {
|
|
656
|
+
if (c === '_') {
|
|
657
|
+
hasLetter = true;
|
|
658
|
+
}
|
|
659
|
+
continue;
|
|
660
|
+
} else if (i === this.pos || !hasLetter || c !== '_' && (c < '0' || c > '9')) {
|
|
661
|
+
break;
|
|
662
|
+
}
|
|
663
|
+
} else {
|
|
664
|
+
hasLetter = true;
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
if (hasLetter) {
|
|
668
|
+
var str = this.expression.substring(startPos, i);
|
|
669
|
+
this.current = this.newToken(TNAME, str);
|
|
670
|
+
this.pos += str.length;
|
|
671
|
+
return true;
|
|
672
|
+
}
|
|
673
|
+
return false;
|
|
674
|
+
};
|
|
675
|
+
TokenStream.prototype.isWhitespace = function () {
|
|
676
|
+
var r = false;
|
|
677
|
+
var c = this.expression.charAt(this.pos);
|
|
678
|
+
while (c === ' ' || c === '\t' || c === '\n' || c === '\r') {
|
|
679
|
+
r = true;
|
|
680
|
+
this.pos++;
|
|
681
|
+
if (this.pos >= this.expression.length) {
|
|
682
|
+
break;
|
|
683
|
+
}
|
|
684
|
+
c = this.expression.charAt(this.pos);
|
|
685
|
+
}
|
|
686
|
+
return r;
|
|
687
|
+
};
|
|
688
|
+
var codePointPattern = /^[0-9a-f]{4}$/i;
|
|
689
|
+
TokenStream.prototype.unescape = function (v) {
|
|
690
|
+
var index = v.indexOf('\\');
|
|
691
|
+
if (index < 0) {
|
|
692
|
+
return v;
|
|
693
|
+
}
|
|
694
|
+
var buffer = v.substring(0, index);
|
|
695
|
+
while (index >= 0) {
|
|
696
|
+
var c = v.charAt(++index);
|
|
697
|
+
switch (c) {
|
|
698
|
+
case '\'':
|
|
699
|
+
buffer += '\'';
|
|
700
|
+
break;
|
|
701
|
+
case '"':
|
|
702
|
+
buffer += '"';
|
|
703
|
+
break;
|
|
704
|
+
case '\\':
|
|
705
|
+
buffer += '\\';
|
|
706
|
+
break;
|
|
707
|
+
case '/':
|
|
708
|
+
buffer += '/';
|
|
709
|
+
break;
|
|
710
|
+
case 'b':
|
|
711
|
+
buffer += '\b';
|
|
712
|
+
break;
|
|
713
|
+
case 'f':
|
|
714
|
+
buffer += '\f';
|
|
715
|
+
break;
|
|
716
|
+
case 'n':
|
|
717
|
+
buffer += '\n';
|
|
718
|
+
break;
|
|
719
|
+
case 'r':
|
|
720
|
+
buffer += '\r';
|
|
721
|
+
break;
|
|
722
|
+
case 't':
|
|
723
|
+
buffer += '\t';
|
|
724
|
+
break;
|
|
725
|
+
case 'u':
|
|
726
|
+
// interpret the following 4 characters as the hex of the unicode code point
|
|
727
|
+
var codePoint = v.substring(index + 1, index + 5);
|
|
728
|
+
if (!codePointPattern.test(codePoint)) {
|
|
729
|
+
this.parseError("Illegal escape sequence: \\u" + codePoint);
|
|
730
|
+
}
|
|
731
|
+
buffer += String.fromCharCode(parseInt(codePoint, 16));
|
|
732
|
+
index += 4;
|
|
733
|
+
break;
|
|
734
|
+
default:
|
|
735
|
+
throw this.parseError('Illegal escape sequence: "\\' + c + '"');
|
|
736
|
+
}
|
|
737
|
+
++index;
|
|
738
|
+
var backslash = v.indexOf('\\', index);
|
|
739
|
+
buffer += v.substring(index, backslash < 0 ? v.length : backslash);
|
|
740
|
+
index = backslash;
|
|
741
|
+
}
|
|
742
|
+
return buffer;
|
|
743
|
+
};
|
|
744
|
+
TokenStream.prototype.isComment = function () {
|
|
745
|
+
var c = this.expression.charAt(this.pos);
|
|
746
|
+
if (c === '/' && this.expression.charAt(this.pos + 1) === '*') {
|
|
747
|
+
this.pos = this.expression.indexOf('*/', this.pos) + 2;
|
|
748
|
+
if (this.pos === 1) {
|
|
749
|
+
this.pos = this.expression.length;
|
|
750
|
+
}
|
|
751
|
+
return true;
|
|
752
|
+
}
|
|
753
|
+
return false;
|
|
754
|
+
};
|
|
755
|
+
TokenStream.prototype.isRadixInteger = function () {
|
|
756
|
+
var pos = this.pos;
|
|
757
|
+
if (pos >= this.expression.length - 2 || this.expression.charAt(pos) !== '0') {
|
|
758
|
+
return false;
|
|
759
|
+
}
|
|
760
|
+
++pos;
|
|
761
|
+
var radix;
|
|
762
|
+
var validDigit;
|
|
763
|
+
if (this.expression.charAt(pos) === 'x') {
|
|
764
|
+
radix = 16;
|
|
765
|
+
validDigit = /^[0-9a-f]$/i;
|
|
766
|
+
++pos;
|
|
767
|
+
} else if (this.expression.charAt(pos) === 'b') {
|
|
768
|
+
radix = 2;
|
|
769
|
+
validDigit = /^[01]$/i;
|
|
770
|
+
++pos;
|
|
771
|
+
} else {
|
|
772
|
+
return false;
|
|
773
|
+
}
|
|
774
|
+
var valid = false;
|
|
775
|
+
var startPos = pos;
|
|
776
|
+
while (pos < this.expression.length) {
|
|
777
|
+
var c = this.expression.charAt(pos);
|
|
778
|
+
if (validDigit.test(c)) {
|
|
779
|
+
pos++;
|
|
780
|
+
valid = true;
|
|
781
|
+
} else {
|
|
782
|
+
break;
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
if (valid) {
|
|
786
|
+
this.current = this.newToken(TNUMBER, parseInt(this.expression.substring(startPos, pos), radix));
|
|
787
|
+
this.pos = pos;
|
|
788
|
+
}
|
|
789
|
+
return valid;
|
|
790
|
+
};
|
|
791
|
+
TokenStream.prototype.isNumber = function () {
|
|
792
|
+
var valid = false;
|
|
793
|
+
var pos = this.pos;
|
|
794
|
+
var startPos = pos;
|
|
795
|
+
var resetPos = pos;
|
|
796
|
+
var foundDot = false;
|
|
797
|
+
var foundDigits = false;
|
|
798
|
+
var c;
|
|
799
|
+
while (pos < this.expression.length) {
|
|
800
|
+
c = this.expression.charAt(pos);
|
|
801
|
+
if (c >= '0' && c <= '9' || !foundDot && c === '.') {
|
|
802
|
+
if (c === '.') {
|
|
803
|
+
foundDot = true;
|
|
804
|
+
} else {
|
|
805
|
+
foundDigits = true;
|
|
806
|
+
}
|
|
807
|
+
pos++;
|
|
808
|
+
valid = foundDigits;
|
|
809
|
+
} else {
|
|
810
|
+
break;
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
if (valid) {
|
|
814
|
+
resetPos = pos;
|
|
815
|
+
}
|
|
816
|
+
if (c === 'e' || c === 'E') {
|
|
817
|
+
pos++;
|
|
818
|
+
var acceptSign = true;
|
|
819
|
+
var validExponent = false;
|
|
820
|
+
while (pos < this.expression.length) {
|
|
821
|
+
c = this.expression.charAt(pos);
|
|
822
|
+
if (acceptSign && (c === '+' || c === '-')) {
|
|
823
|
+
acceptSign = false;
|
|
824
|
+
} else if (c >= '0' && c <= '9') {
|
|
825
|
+
validExponent = true;
|
|
826
|
+
acceptSign = false;
|
|
827
|
+
} else {
|
|
828
|
+
break;
|
|
829
|
+
}
|
|
830
|
+
pos++;
|
|
831
|
+
}
|
|
832
|
+
if (!validExponent) {
|
|
833
|
+
pos = resetPos;
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
if (valid) {
|
|
837
|
+
this.current = this.newToken(TNUMBER, parseFloat(this.expression.substring(startPos, pos)));
|
|
838
|
+
this.pos = pos;
|
|
839
|
+
} else {
|
|
840
|
+
this.pos = resetPos;
|
|
841
|
+
}
|
|
842
|
+
return valid;
|
|
843
|
+
};
|
|
844
|
+
TokenStream.prototype.isOperator = function () {
|
|
845
|
+
var startPos = this.pos;
|
|
846
|
+
var c = this.expression.charAt(this.pos);
|
|
847
|
+
if (c === '+' || c === '-' || c === '*' || c === '/' || c === '%' || c === '^' || c === '?' || c === ':' || c === '.') {
|
|
848
|
+
this.current = this.newToken(TOP, c);
|
|
849
|
+
} else if (c === '∙' || c === '•') {
|
|
850
|
+
this.current = this.newToken(TOP, '*');
|
|
851
|
+
} else if (c === '>') {
|
|
852
|
+
if (this.expression.charAt(this.pos + 1) === '=') {
|
|
853
|
+
this.current = this.newToken(TOP, '>=');
|
|
854
|
+
this.pos++;
|
|
855
|
+
} else {
|
|
856
|
+
this.current = this.newToken(TOP, '>');
|
|
857
|
+
}
|
|
858
|
+
} else if (c === '<') {
|
|
859
|
+
if (this.expression.charAt(this.pos + 1) === '=') {
|
|
860
|
+
this.current = this.newToken(TOP, '<=');
|
|
861
|
+
this.pos++;
|
|
862
|
+
} else {
|
|
863
|
+
this.current = this.newToken(TOP, '<');
|
|
864
|
+
}
|
|
865
|
+
} else if (c === '|') {
|
|
866
|
+
if (this.expression.charAt(this.pos + 1) === '|') {
|
|
867
|
+
this.current = this.newToken(TOP, '||');
|
|
868
|
+
this.pos++;
|
|
869
|
+
} else {
|
|
870
|
+
return false;
|
|
871
|
+
}
|
|
872
|
+
} else if (c === '=') {
|
|
873
|
+
if (this.expression.charAt(this.pos + 1) === '=') {
|
|
874
|
+
this.current = this.newToken(TOP, '==');
|
|
875
|
+
this.pos++;
|
|
876
|
+
} else {
|
|
877
|
+
this.current = this.newToken(TOP, c);
|
|
878
|
+
}
|
|
879
|
+
} else if (c === '!') {
|
|
880
|
+
if (this.expression.charAt(this.pos + 1) === '=') {
|
|
881
|
+
this.current = this.newToken(TOP, '!=');
|
|
882
|
+
this.pos++;
|
|
883
|
+
} else {
|
|
884
|
+
this.current = this.newToken(TOP, c);
|
|
885
|
+
}
|
|
886
|
+
} else {
|
|
887
|
+
return false;
|
|
888
|
+
}
|
|
889
|
+
this.pos++;
|
|
890
|
+
if (this.isOperatorEnabled(this.current.value)) {
|
|
891
|
+
return true;
|
|
892
|
+
} else {
|
|
893
|
+
this.pos = startPos;
|
|
894
|
+
return false;
|
|
895
|
+
}
|
|
896
|
+
};
|
|
897
|
+
TokenStream.prototype.isOperatorEnabled = function (op) {
|
|
898
|
+
return this.parser.isOperatorEnabled(op);
|
|
899
|
+
};
|
|
900
|
+
TokenStream.prototype.getCoordinates = function () {
|
|
901
|
+
var line = 0;
|
|
902
|
+
var column;
|
|
903
|
+
var newline = -1;
|
|
904
|
+
do {
|
|
905
|
+
line++;
|
|
906
|
+
column = this.pos - newline;
|
|
907
|
+
newline = this.expression.indexOf('\n', newline + 1);
|
|
908
|
+
} while (newline >= 0 && newline < this.pos);
|
|
909
|
+
return {
|
|
910
|
+
line: line,
|
|
911
|
+
column: column
|
|
912
|
+
};
|
|
913
|
+
};
|
|
914
|
+
TokenStream.prototype.parseError = function (msg) {
|
|
915
|
+
var coords = this.getCoordinates();
|
|
916
|
+
throw new Error('parse error [' + coords.line + ':' + coords.column + ']: ' + msg);
|
|
917
|
+
};
|
|
918
|
+
function ParserState(parser, tokenStream, options) {
|
|
919
|
+
this.parser = parser;
|
|
920
|
+
this.tokens = tokenStream;
|
|
921
|
+
this.current = null;
|
|
922
|
+
this.nextToken = null;
|
|
923
|
+
this.next();
|
|
924
|
+
this.savedCurrent = null;
|
|
925
|
+
this.savedNextToken = null;
|
|
926
|
+
this.allowMemberAccess = options.allowMemberAccess !== false;
|
|
927
|
+
}
|
|
928
|
+
ParserState.prototype.next = function () {
|
|
929
|
+
this.current = this.nextToken;
|
|
930
|
+
return this.nextToken = this.tokens.next();
|
|
931
|
+
};
|
|
932
|
+
ParserState.prototype.tokenMatches = function (token, value) {
|
|
933
|
+
if (typeof value === 'undefined') {
|
|
934
|
+
return true;
|
|
935
|
+
} else if (Array.isArray(value)) {
|
|
936
|
+
return contains(value, token.value);
|
|
937
|
+
} else if (typeof value === 'function') {
|
|
938
|
+
return value(token);
|
|
939
|
+
} else {
|
|
940
|
+
return token.value === value;
|
|
941
|
+
}
|
|
942
|
+
};
|
|
943
|
+
ParserState.prototype.save = function () {
|
|
944
|
+
this.savedCurrent = this.current;
|
|
945
|
+
this.savedNextToken = this.nextToken;
|
|
946
|
+
this.tokens.save();
|
|
947
|
+
};
|
|
948
|
+
ParserState.prototype.restore = function () {
|
|
949
|
+
this.tokens.restore();
|
|
950
|
+
this.current = this.savedCurrent;
|
|
951
|
+
this.nextToken = this.savedNextToken;
|
|
952
|
+
};
|
|
953
|
+
ParserState.prototype.accept = function (type, value) {
|
|
954
|
+
if (this.nextToken.type === type && this.tokenMatches(this.nextToken, value)) {
|
|
955
|
+
this.next();
|
|
956
|
+
return true;
|
|
957
|
+
}
|
|
958
|
+
return false;
|
|
959
|
+
};
|
|
960
|
+
ParserState.prototype.expect = function (type, value) {
|
|
961
|
+
if (!this.accept(type, value)) {
|
|
962
|
+
var coords = this.tokens.getCoordinates();
|
|
963
|
+
throw new Error('parse error [' + coords.line + ':' + coords.column + ']: Expected ' + (value || type));
|
|
964
|
+
}
|
|
965
|
+
};
|
|
966
|
+
ParserState.prototype.parseAtom = function (instr) {
|
|
967
|
+
var unaryOps = this.tokens.unaryOps;
|
|
968
|
+
function isPrefixOperator(token) {
|
|
969
|
+
return token.value in unaryOps;
|
|
970
|
+
}
|
|
971
|
+
if (this.accept(TNAME) || this.accept(TOP, isPrefixOperator)) {
|
|
972
|
+
instr.push(new Instruction(IVAR, this.current.value));
|
|
973
|
+
} else if (this.accept(TNUMBER)) {
|
|
974
|
+
instr.push(new Instruction(INUMBER, this.current.value));
|
|
975
|
+
} else if (this.accept(TSTRING)) {
|
|
976
|
+
instr.push(new Instruction(INUMBER, this.current.value));
|
|
977
|
+
} else if (this.accept(TPAREN, '(')) {
|
|
978
|
+
this.parseExpression(instr);
|
|
979
|
+
this.expect(TPAREN, ')');
|
|
980
|
+
} else if (this.accept(TBRACKET, '[')) {
|
|
981
|
+
if (this.accept(TBRACKET, ']')) {
|
|
982
|
+
instr.push(new Instruction(IARRAY, 0));
|
|
983
|
+
} else {
|
|
984
|
+
var argCount = this.parseArrayList(instr);
|
|
985
|
+
instr.push(new Instruction(IARRAY, argCount));
|
|
986
|
+
}
|
|
987
|
+
} else {
|
|
988
|
+
throw new Error('unexpected ' + this.nextToken);
|
|
989
|
+
}
|
|
990
|
+
};
|
|
991
|
+
ParserState.prototype.parseExpression = function (instr) {
|
|
992
|
+
var exprInstr = [];
|
|
993
|
+
if (this.parseUntilEndStatement(instr, exprInstr)) {
|
|
994
|
+
return;
|
|
995
|
+
}
|
|
996
|
+
this.parseVariableAssignmentExpression(exprInstr);
|
|
997
|
+
if (this.parseUntilEndStatement(instr, exprInstr)) {
|
|
998
|
+
return;
|
|
999
|
+
}
|
|
1000
|
+
this.pushExpression(instr, exprInstr);
|
|
1001
|
+
};
|
|
1002
|
+
ParserState.prototype.pushExpression = function (instr, exprInstr) {
|
|
1003
|
+
for (var i = 0, len = exprInstr.length; i < len; i++) {
|
|
1004
|
+
instr.push(exprInstr[i]);
|
|
1005
|
+
}
|
|
1006
|
+
};
|
|
1007
|
+
ParserState.prototype.parseUntilEndStatement = function (instr, exprInstr) {
|
|
1008
|
+
if (!this.accept(TSEMICOLON)) return false;
|
|
1009
|
+
if (this.nextToken && this.nextToken.type !== TEOF && !(this.nextToken.type === TPAREN && this.nextToken.value === ')')) {
|
|
1010
|
+
exprInstr.push(new Instruction(IENDSTATEMENT));
|
|
1011
|
+
}
|
|
1012
|
+
if (this.nextToken.type !== TEOF) {
|
|
1013
|
+
this.parseExpression(exprInstr);
|
|
1014
|
+
}
|
|
1015
|
+
instr.push(new Instruction(IEXPR, exprInstr));
|
|
1016
|
+
return true;
|
|
1017
|
+
};
|
|
1018
|
+
ParserState.prototype.parseArrayList = function (instr) {
|
|
1019
|
+
var argCount = 0;
|
|
1020
|
+
while (!this.accept(TBRACKET, ']')) {
|
|
1021
|
+
this.parseExpression(instr);
|
|
1022
|
+
++argCount;
|
|
1023
|
+
while (this.accept(TCOMMA)) {
|
|
1024
|
+
this.parseExpression(instr);
|
|
1025
|
+
++argCount;
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
1028
|
+
return argCount;
|
|
1029
|
+
};
|
|
1030
|
+
ParserState.prototype.parseVariableAssignmentExpression = function (instr) {
|
|
1031
|
+
this.parseConditionalExpression(instr);
|
|
1032
|
+
while (this.accept(TOP, '=')) {
|
|
1033
|
+
var varName = instr.pop();
|
|
1034
|
+
var varValue = [];
|
|
1035
|
+
var lastInstrIndex = instr.length - 1;
|
|
1036
|
+
if (varName.type === IFUNCALL) {
|
|
1037
|
+
if (!this.tokens.isOperatorEnabled('()=')) {
|
|
1038
|
+
throw new Error('function definition is not permitted');
|
|
1039
|
+
}
|
|
1040
|
+
for (var i = 0, len = varName.value + 1; i < len; i++) {
|
|
1041
|
+
var index = lastInstrIndex - i;
|
|
1042
|
+
if (instr[index].type === IVAR) {
|
|
1043
|
+
instr[index] = new Instruction(IVARNAME, instr[index].value);
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
this.parseVariableAssignmentExpression(varValue);
|
|
1047
|
+
instr.push(new Instruction(IEXPR, varValue));
|
|
1048
|
+
instr.push(new Instruction(IFUNDEF, varName.value));
|
|
1049
|
+
continue;
|
|
1050
|
+
}
|
|
1051
|
+
if (varName.type !== IVAR && varName.type !== IMEMBER) {
|
|
1052
|
+
throw new Error('expected variable for assignment');
|
|
1053
|
+
}
|
|
1054
|
+
this.parseVariableAssignmentExpression(varValue);
|
|
1055
|
+
instr.push(new Instruction(IVARNAME, varName.value));
|
|
1056
|
+
instr.push(new Instruction(IEXPR, varValue));
|
|
1057
|
+
instr.push(binaryInstruction('='));
|
|
1058
|
+
}
|
|
1059
|
+
};
|
|
1060
|
+
ParserState.prototype.parseConditionalExpression = function (instr) {
|
|
1061
|
+
this.parseOrExpression(instr);
|
|
1062
|
+
while (this.accept(TOP, '?')) {
|
|
1063
|
+
var trueBranch = [];
|
|
1064
|
+
var falseBranch = [];
|
|
1065
|
+
this.parseConditionalExpression(trueBranch);
|
|
1066
|
+
this.expect(TOP, ':');
|
|
1067
|
+
this.parseConditionalExpression(falseBranch);
|
|
1068
|
+
instr.push(new Instruction(IEXPR, trueBranch));
|
|
1069
|
+
instr.push(new Instruction(IEXPR, falseBranch));
|
|
1070
|
+
instr.push(ternaryInstruction('?'));
|
|
1071
|
+
}
|
|
1072
|
+
};
|
|
1073
|
+
ParserState.prototype.parseOrExpression = function (instr) {
|
|
1074
|
+
this.parseAndExpression(instr);
|
|
1075
|
+
while (this.accept(TOP, 'or')) {
|
|
1076
|
+
var falseBranch = [];
|
|
1077
|
+
this.parseAndExpression(falseBranch);
|
|
1078
|
+
instr.push(new Instruction(IEXPR, falseBranch));
|
|
1079
|
+
instr.push(binaryInstruction('or'));
|
|
1080
|
+
}
|
|
1081
|
+
};
|
|
1082
|
+
ParserState.prototype.parseAndExpression = function (instr) {
|
|
1083
|
+
this.parseComparison(instr);
|
|
1084
|
+
while (this.accept(TOP, 'and')) {
|
|
1085
|
+
var trueBranch = [];
|
|
1086
|
+
this.parseComparison(trueBranch);
|
|
1087
|
+
instr.push(new Instruction(IEXPR, trueBranch));
|
|
1088
|
+
instr.push(binaryInstruction('and'));
|
|
1089
|
+
}
|
|
1090
|
+
};
|
|
1091
|
+
var COMPARISON_OPERATORS = ['==', '!=', '<', '<=', '>=', '>', 'in'];
|
|
1092
|
+
ParserState.prototype.parseComparison = function (instr) {
|
|
1093
|
+
this.parseAddSub(instr);
|
|
1094
|
+
while (this.accept(TOP, COMPARISON_OPERATORS)) {
|
|
1095
|
+
var op = this.current;
|
|
1096
|
+
this.parseAddSub(instr);
|
|
1097
|
+
instr.push(binaryInstruction(op.value));
|
|
1098
|
+
}
|
|
1099
|
+
};
|
|
1100
|
+
var ADD_SUB_OPERATORS = ['+', '-', '||'];
|
|
1101
|
+
ParserState.prototype.parseAddSub = function (instr) {
|
|
1102
|
+
this.parseTerm(instr);
|
|
1103
|
+
while (this.accept(TOP, ADD_SUB_OPERATORS)) {
|
|
1104
|
+
var op = this.current;
|
|
1105
|
+
this.parseTerm(instr);
|
|
1106
|
+
instr.push(binaryInstruction(op.value));
|
|
1107
|
+
}
|
|
1108
|
+
};
|
|
1109
|
+
var TERM_OPERATORS = ['*', '/', '%'];
|
|
1110
|
+
ParserState.prototype.parseTerm = function (instr) {
|
|
1111
|
+
this.parseFactor(instr);
|
|
1112
|
+
while (this.accept(TOP, TERM_OPERATORS)) {
|
|
1113
|
+
var op = this.current;
|
|
1114
|
+
this.parseFactor(instr);
|
|
1115
|
+
instr.push(binaryInstruction(op.value));
|
|
1116
|
+
}
|
|
1117
|
+
};
|
|
1118
|
+
ParserState.prototype.parseFactor = function (instr) {
|
|
1119
|
+
var unaryOps = this.tokens.unaryOps;
|
|
1120
|
+
function isPrefixOperator(token) {
|
|
1121
|
+
return token.value in unaryOps;
|
|
1122
|
+
}
|
|
1123
|
+
this.save();
|
|
1124
|
+
if (this.accept(TOP, isPrefixOperator)) {
|
|
1125
|
+
if (this.current.value !== '-' && this.current.value !== '+') {
|
|
1126
|
+
if (this.nextToken.type === TPAREN && this.nextToken.value === '(') {
|
|
1127
|
+
this.restore();
|
|
1128
|
+
this.parseExponential(instr);
|
|
1129
|
+
return;
|
|
1130
|
+
} else if (this.nextToken.type === TSEMICOLON || this.nextToken.type === TCOMMA || this.nextToken.type === TEOF || this.nextToken.type === TPAREN && this.nextToken.value === ')') {
|
|
1131
|
+
this.restore();
|
|
1132
|
+
this.parseAtom(instr);
|
|
1133
|
+
return;
|
|
1134
|
+
}
|
|
1135
|
+
}
|
|
1136
|
+
var op = this.current;
|
|
1137
|
+
this.parseFactor(instr);
|
|
1138
|
+
instr.push(unaryInstruction(op.value));
|
|
1139
|
+
} else {
|
|
1140
|
+
this.parseExponential(instr);
|
|
1141
|
+
}
|
|
1142
|
+
};
|
|
1143
|
+
ParserState.prototype.parseExponential = function (instr) {
|
|
1144
|
+
this.parsePostfixExpression(instr);
|
|
1145
|
+
while (this.accept(TOP, '^')) {
|
|
1146
|
+
this.parseFactor(instr);
|
|
1147
|
+
instr.push(binaryInstruction('^'));
|
|
1148
|
+
}
|
|
1149
|
+
};
|
|
1150
|
+
ParserState.prototype.parsePostfixExpression = function (instr) {
|
|
1151
|
+
this.parseFunctionCall(instr);
|
|
1152
|
+
while (this.accept(TOP, '!')) {
|
|
1153
|
+
instr.push(unaryInstruction('!'));
|
|
1154
|
+
}
|
|
1155
|
+
};
|
|
1156
|
+
ParserState.prototype.parseFunctionCall = function (instr) {
|
|
1157
|
+
var unaryOps = this.tokens.unaryOps;
|
|
1158
|
+
function isPrefixOperator(token) {
|
|
1159
|
+
return token.value in unaryOps;
|
|
1160
|
+
}
|
|
1161
|
+
if (this.accept(TOP, isPrefixOperator)) {
|
|
1162
|
+
var op = this.current;
|
|
1163
|
+
this.parseAtom(instr);
|
|
1164
|
+
instr.push(unaryInstruction(op.value));
|
|
1165
|
+
} else {
|
|
1166
|
+
this.parseMemberExpression(instr);
|
|
1167
|
+
while (this.accept(TPAREN, '(')) {
|
|
1168
|
+
if (this.accept(TPAREN, ')')) {
|
|
1169
|
+
instr.push(new Instruction(IFUNCALL, 0));
|
|
1170
|
+
} else {
|
|
1171
|
+
var argCount = this.parseArgumentList(instr);
|
|
1172
|
+
instr.push(new Instruction(IFUNCALL, argCount));
|
|
1173
|
+
}
|
|
1174
|
+
}
|
|
1175
|
+
}
|
|
1176
|
+
};
|
|
1177
|
+
ParserState.prototype.parseArgumentList = function (instr) {
|
|
1178
|
+
var argCount = 0;
|
|
1179
|
+
while (!this.accept(TPAREN, ')')) {
|
|
1180
|
+
this.parseExpression(instr);
|
|
1181
|
+
++argCount;
|
|
1182
|
+
while (this.accept(TCOMMA)) {
|
|
1183
|
+
this.parseExpression(instr);
|
|
1184
|
+
++argCount;
|
|
1185
|
+
}
|
|
1186
|
+
}
|
|
1187
|
+
return argCount;
|
|
1188
|
+
};
|
|
1189
|
+
ParserState.prototype.parseMemberExpression = function (instr) {
|
|
1190
|
+
this.parseAtom(instr);
|
|
1191
|
+
while (this.accept(TOP, '.') || this.accept(TBRACKET, '[')) {
|
|
1192
|
+
var op = this.current;
|
|
1193
|
+
if (op.value === '.') {
|
|
1194
|
+
if (!this.allowMemberAccess) {
|
|
1195
|
+
throw new Error('unexpected ".", member access is not permitted');
|
|
1196
|
+
}
|
|
1197
|
+
this.expect(TNAME);
|
|
1198
|
+
instr.push(new Instruction(IMEMBER, this.current.value));
|
|
1199
|
+
} else if (op.value === '[') {
|
|
1200
|
+
if (!this.tokens.isOperatorEnabled('[')) {
|
|
1201
|
+
throw new Error('unexpected "[]", arrays are disabled');
|
|
1202
|
+
}
|
|
1203
|
+
this.parseExpression(instr);
|
|
1204
|
+
this.expect(TBRACKET, ']');
|
|
1205
|
+
instr.push(binaryInstruction('['));
|
|
1206
|
+
} else {
|
|
1207
|
+
throw new Error('unexpected symbol: ' + op.value);
|
|
1208
|
+
}
|
|
1209
|
+
}
|
|
1210
|
+
};
|
|
1211
|
+
function add(a, b) {
|
|
1212
|
+
return Number(a) + Number(b);
|
|
1213
|
+
}
|
|
1214
|
+
function sub(a, b) {
|
|
1215
|
+
return a - b;
|
|
1216
|
+
}
|
|
1217
|
+
function mul(a, b) {
|
|
1218
|
+
return a * b;
|
|
1219
|
+
}
|
|
1220
|
+
function div(a, b) {
|
|
1221
|
+
return a / b;
|
|
1222
|
+
}
|
|
1223
|
+
function mod(a, b) {
|
|
1224
|
+
return a % b;
|
|
1225
|
+
}
|
|
1226
|
+
function concat(a, b) {
|
|
1227
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
1228
|
+
return a.concat(b);
|
|
1229
|
+
}
|
|
1230
|
+
return '' + a + b;
|
|
1231
|
+
}
|
|
1232
|
+
function equal(a, b) {
|
|
1233
|
+
return a === b;
|
|
1234
|
+
}
|
|
1235
|
+
function notEqual(a, b) {
|
|
1236
|
+
return a !== b;
|
|
1237
|
+
}
|
|
1238
|
+
function greaterThan(a, b) {
|
|
1239
|
+
return a > b;
|
|
1240
|
+
}
|
|
1241
|
+
function lessThan(a, b) {
|
|
1242
|
+
return a < b;
|
|
1243
|
+
}
|
|
1244
|
+
function greaterThanEqual(a, b) {
|
|
1245
|
+
return a >= b;
|
|
1246
|
+
}
|
|
1247
|
+
function lessThanEqual(a, b) {
|
|
1248
|
+
return a <= b;
|
|
1249
|
+
}
|
|
1250
|
+
function andOperator(a, b) {
|
|
1251
|
+
return Boolean(a && b);
|
|
1252
|
+
}
|
|
1253
|
+
function orOperator(a, b) {
|
|
1254
|
+
return Boolean(a || b);
|
|
1255
|
+
}
|
|
1256
|
+
function inOperator(a, b) {
|
|
1257
|
+
return contains(b, a);
|
|
1258
|
+
}
|
|
1259
|
+
function sinh(a) {
|
|
1260
|
+
return (Math.exp(a) - Math.exp(-a)) / 2;
|
|
1261
|
+
}
|
|
1262
|
+
function cosh(a) {
|
|
1263
|
+
return (Math.exp(a) + Math.exp(-a)) / 2;
|
|
1264
|
+
}
|
|
1265
|
+
function tanh(a) {
|
|
1266
|
+
if (a === Infinity) return 1;
|
|
1267
|
+
if (a === -Infinity) return -1;
|
|
1268
|
+
return (Math.exp(a) - Math.exp(-a)) / (Math.exp(a) + Math.exp(-a));
|
|
1269
|
+
}
|
|
1270
|
+
function asinh(a) {
|
|
1271
|
+
if (a === -Infinity) return a;
|
|
1272
|
+
return Math.log(a + Math.sqrt(a * a + 1));
|
|
1273
|
+
}
|
|
1274
|
+
function acosh(a) {
|
|
1275
|
+
return Math.log(a + Math.sqrt(a * a - 1));
|
|
1276
|
+
}
|
|
1277
|
+
function atanh(a) {
|
|
1278
|
+
return Math.log((1 + a) / (1 - a)) / 2;
|
|
1279
|
+
}
|
|
1280
|
+
function log10(a) {
|
|
1281
|
+
return Math.log(a) * Math.LOG10E;
|
|
1282
|
+
}
|
|
1283
|
+
function neg(a) {
|
|
1284
|
+
return -a;
|
|
1285
|
+
}
|
|
1286
|
+
function not(a) {
|
|
1287
|
+
return !a;
|
|
1288
|
+
}
|
|
1289
|
+
function trunc(a) {
|
|
1290
|
+
return a < 0 ? Math.ceil(a) : Math.floor(a);
|
|
1291
|
+
}
|
|
1292
|
+
function random(a) {
|
|
1293
|
+
return Math.random() * (a || 1);
|
|
1294
|
+
}
|
|
1295
|
+
function factorial(a) {
|
|
1296
|
+
// a!
|
|
1297
|
+
return gamma(a + 1);
|
|
1298
|
+
}
|
|
1299
|
+
function isInteger(value) {
|
|
1300
|
+
return isFinite(value) && value === Math.round(value);
|
|
1301
|
+
}
|
|
1302
|
+
var GAMMA_G = 4.7421875;
|
|
1303
|
+
var GAMMA_P = [0.99999999999999709182, 57.156235665862923517, -59.597960355475491248, 14.136097974741747174, -0.49191381609762019978, 0.33994649984811888699e-4, 0.46523628927048575665e-4, -0.98374475304879564677e-4, 0.15808870322491248884e-3, -0.21026444172410488319e-3, 0.21743961811521264320e-3, -0.16431810653676389022e-3, 0.84418223983852743293e-4, -0.26190838401581408670e-4, 0.36899182659531622704e-5];
|
|
1304
|
+
|
|
1305
|
+
// Gamma function from math.js
|
|
1306
|
+
function gamma(n) {
|
|
1307
|
+
var t, x;
|
|
1308
|
+
if (isInteger(n)) {
|
|
1309
|
+
if (n <= 0) {
|
|
1310
|
+
return isFinite(n) ? Infinity : NaN;
|
|
1311
|
+
}
|
|
1312
|
+
if (n > 171) {
|
|
1313
|
+
return Infinity; // Will overflow
|
|
1314
|
+
}
|
|
1315
|
+
var value = n - 2;
|
|
1316
|
+
var res = n - 1;
|
|
1317
|
+
while (value > 1) {
|
|
1318
|
+
res *= value;
|
|
1319
|
+
value--;
|
|
1320
|
+
}
|
|
1321
|
+
if (res === 0) {
|
|
1322
|
+
res = 1; // 0! is per definition 1
|
|
1323
|
+
}
|
|
1324
|
+
return res;
|
|
1325
|
+
}
|
|
1326
|
+
if (n < 0.5) {
|
|
1327
|
+
return Math.PI / (Math.sin(Math.PI * n) * gamma(1 - n));
|
|
1328
|
+
}
|
|
1329
|
+
if (n >= 171.35) {
|
|
1330
|
+
return Infinity; // will overflow
|
|
1331
|
+
}
|
|
1332
|
+
if (n > 85.0) {
|
|
1333
|
+
// Extended Stirling Approx
|
|
1334
|
+
var twoN = n * n;
|
|
1335
|
+
var threeN = twoN * n;
|
|
1336
|
+
var fourN = threeN * n;
|
|
1337
|
+
var fiveN = fourN * n;
|
|
1338
|
+
return Math.sqrt(2 * Math.PI / n) * Math.pow(n / Math.E, n) * (1 + 1 / (12 * n) + 1 / (288 * twoN) - 139 / (51840 * threeN) - 571 / (2488320 * fourN) + 163879 / (209018880 * fiveN) + 5246819 / (75246796800 * fiveN * n));
|
|
1339
|
+
}
|
|
1340
|
+
--n;
|
|
1341
|
+
x = GAMMA_P[0];
|
|
1342
|
+
for (var i = 1; i < GAMMA_P.length; ++i) {
|
|
1343
|
+
x += GAMMA_P[i] / (n + i);
|
|
1344
|
+
}
|
|
1345
|
+
t = n + GAMMA_G + 0.5;
|
|
1346
|
+
return Math.sqrt(2 * Math.PI) * Math.pow(t, n + 0.5) * Math.exp(-t) * x;
|
|
1347
|
+
}
|
|
1348
|
+
function stringOrArrayLength(s) {
|
|
1349
|
+
if (Array.isArray(s)) {
|
|
1350
|
+
return s.length;
|
|
1351
|
+
}
|
|
1352
|
+
return String(s).length;
|
|
1353
|
+
}
|
|
1354
|
+
function hypot() {
|
|
1355
|
+
var sum = 0;
|
|
1356
|
+
var larg = 0;
|
|
1357
|
+
for (var i = 0; i < arguments.length; i++) {
|
|
1358
|
+
var arg = Math.abs(arguments[i]);
|
|
1359
|
+
var div;
|
|
1360
|
+
if (larg < arg) {
|
|
1361
|
+
div = larg / arg;
|
|
1362
|
+
sum = sum * div * div + 1;
|
|
1363
|
+
larg = arg;
|
|
1364
|
+
} else if (arg > 0) {
|
|
1365
|
+
div = arg / larg;
|
|
1366
|
+
sum += div * div;
|
|
1367
|
+
} else {
|
|
1368
|
+
sum += arg;
|
|
1369
|
+
}
|
|
1370
|
+
}
|
|
1371
|
+
return larg === Infinity ? Infinity : larg * Math.sqrt(sum);
|
|
1372
|
+
}
|
|
1373
|
+
function condition(cond, yep, nope) {
|
|
1374
|
+
return cond ? yep : nope;
|
|
1375
|
+
}
|
|
1376
|
+
|
|
1377
|
+
/**
|
|
1378
|
+
* Decimal adjustment of a number.
|
|
1379
|
+
* From @escopecz.
|
|
1380
|
+
*
|
|
1381
|
+
* @param {Number} value The number.
|
|
1382
|
+
* @param {Integer} exp The exponent (the 10 logarithm of the adjustment base).
|
|
1383
|
+
* @return {Number} The adjusted value.
|
|
1384
|
+
*/
|
|
1385
|
+
function roundTo(value, exp) {
|
|
1386
|
+
// If the exp is undefined or zero...
|
|
1387
|
+
if (typeof exp === 'undefined' || +exp === 0) {
|
|
1388
|
+
return Math.round(value);
|
|
1389
|
+
}
|
|
1390
|
+
value = +value;
|
|
1391
|
+
exp = -+exp;
|
|
1392
|
+
// If the value is not a number or the exp is not an integer...
|
|
1393
|
+
if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) {
|
|
1394
|
+
return NaN;
|
|
1395
|
+
}
|
|
1396
|
+
// Shift
|
|
1397
|
+
value = value.toString().split('e');
|
|
1398
|
+
value = Math.round(+(value[0] + 'e' + (value[1] ? +value[1] - exp : -exp)));
|
|
1399
|
+
// Shift back
|
|
1400
|
+
value = value.toString().split('e');
|
|
1401
|
+
return +(value[0] + 'e' + (value[1] ? +value[1] + exp : exp));
|
|
1402
|
+
}
|
|
1403
|
+
function setVar(name, value, variables) {
|
|
1404
|
+
if (variables) variables[name] = value;
|
|
1405
|
+
return value;
|
|
1406
|
+
}
|
|
1407
|
+
function arrayIndex(array, index) {
|
|
1408
|
+
return array[index | 0];
|
|
1409
|
+
}
|
|
1410
|
+
function max(array) {
|
|
1411
|
+
if (arguments.length === 1 && Array.isArray(array)) {
|
|
1412
|
+
return Math.max.apply(Math, array);
|
|
1413
|
+
} else {
|
|
1414
|
+
return Math.max.apply(Math, arguments);
|
|
1415
|
+
}
|
|
1416
|
+
}
|
|
1417
|
+
function min(array) {
|
|
1418
|
+
if (arguments.length === 1 && Array.isArray(array)) {
|
|
1419
|
+
return Math.min.apply(Math, array);
|
|
1420
|
+
} else {
|
|
1421
|
+
return Math.min.apply(Math, arguments);
|
|
1422
|
+
}
|
|
1423
|
+
}
|
|
1424
|
+
function arrayMap(f, a) {
|
|
1425
|
+
if (typeof f !== 'function') {
|
|
1426
|
+
throw new Error('First argument to map is not a function');
|
|
1427
|
+
}
|
|
1428
|
+
if (!Array.isArray(a)) {
|
|
1429
|
+
throw new Error('Second argument to map is not an array');
|
|
1430
|
+
}
|
|
1431
|
+
return a.map(function (x, i) {
|
|
1432
|
+
return f(x, i);
|
|
1433
|
+
});
|
|
1434
|
+
}
|
|
1435
|
+
function arrayFold(f, init, a) {
|
|
1436
|
+
if (typeof f !== 'function') {
|
|
1437
|
+
throw new Error('First argument to fold is not a function');
|
|
1438
|
+
}
|
|
1439
|
+
if (!Array.isArray(a)) {
|
|
1440
|
+
throw new Error('Second argument to fold is not an array');
|
|
1441
|
+
}
|
|
1442
|
+
return a.reduce(function (acc, x, i) {
|
|
1443
|
+
return f(acc, x, i);
|
|
1444
|
+
}, init);
|
|
1445
|
+
}
|
|
1446
|
+
function arrayFilter(f, a) {
|
|
1447
|
+
if (typeof f !== 'function') {
|
|
1448
|
+
throw new Error('First argument to filter is not a function');
|
|
1449
|
+
}
|
|
1450
|
+
if (!Array.isArray(a)) {
|
|
1451
|
+
throw new Error('Second argument to filter is not an array');
|
|
1452
|
+
}
|
|
1453
|
+
return a.filter(function (x, i) {
|
|
1454
|
+
return f(x, i);
|
|
1455
|
+
});
|
|
1456
|
+
}
|
|
1457
|
+
function stringOrArrayIndexOf(target, s) {
|
|
1458
|
+
if (!(Array.isArray(s) || typeof s === 'string')) {
|
|
1459
|
+
throw new Error('Second argument to indexOf is not a string or array');
|
|
1460
|
+
}
|
|
1461
|
+
return s.indexOf(target);
|
|
1462
|
+
}
|
|
1463
|
+
function arrayJoin(sep, a) {
|
|
1464
|
+
if (!Array.isArray(a)) {
|
|
1465
|
+
throw new Error('Second argument to join is not an array');
|
|
1466
|
+
}
|
|
1467
|
+
return a.join(sep);
|
|
1468
|
+
}
|
|
1469
|
+
function sign(x) {
|
|
1470
|
+
return (x > 0) - (x < 0) || +x;
|
|
1471
|
+
}
|
|
1472
|
+
var ONE_THIRD = 1 / 3;
|
|
1473
|
+
function cbrt(x) {
|
|
1474
|
+
return x < 0 ? -Math.pow(-x, ONE_THIRD) : Math.pow(x, ONE_THIRD);
|
|
1475
|
+
}
|
|
1476
|
+
function expm1(x) {
|
|
1477
|
+
return Math.exp(x) - 1;
|
|
1478
|
+
}
|
|
1479
|
+
function log1p(x) {
|
|
1480
|
+
return Math.log(1 + x);
|
|
1481
|
+
}
|
|
1482
|
+
function log2(x) {
|
|
1483
|
+
return Math.log(x) / Math.LN2;
|
|
1484
|
+
}
|
|
1485
|
+
function Parser(options) {
|
|
1486
|
+
this.options = options || {};
|
|
1487
|
+
this.unaryOps = {
|
|
1488
|
+
sin: Math.sin,
|
|
1489
|
+
cos: Math.cos,
|
|
1490
|
+
tan: Math.tan,
|
|
1491
|
+
asin: Math.asin,
|
|
1492
|
+
acos: Math.acos,
|
|
1493
|
+
atan: Math.atan,
|
|
1494
|
+
sinh: Math.sinh || sinh,
|
|
1495
|
+
cosh: Math.cosh || cosh,
|
|
1496
|
+
tanh: Math.tanh || tanh,
|
|
1497
|
+
asinh: Math.asinh || asinh,
|
|
1498
|
+
acosh: Math.acosh || acosh,
|
|
1499
|
+
atanh: Math.atanh || atanh,
|
|
1500
|
+
sqrt: Math.sqrt,
|
|
1501
|
+
cbrt: Math.cbrt || cbrt,
|
|
1502
|
+
log: Math.log,
|
|
1503
|
+
log2: Math.log2 || log2,
|
|
1504
|
+
ln: Math.log,
|
|
1505
|
+
lg: Math.log10 || log10,
|
|
1506
|
+
log10: Math.log10 || log10,
|
|
1507
|
+
expm1: Math.expm1 || expm1,
|
|
1508
|
+
log1p: Math.log1p || log1p,
|
|
1509
|
+
abs: Math.abs,
|
|
1510
|
+
ceil: Math.ceil,
|
|
1511
|
+
floor: Math.floor,
|
|
1512
|
+
round: Math.round,
|
|
1513
|
+
trunc: Math.trunc || trunc,
|
|
1514
|
+
'-': neg,
|
|
1515
|
+
'+': Number,
|
|
1516
|
+
exp: Math.exp,
|
|
1517
|
+
not: not,
|
|
1518
|
+
length: stringOrArrayLength,
|
|
1519
|
+
'!': factorial,
|
|
1520
|
+
sign: Math.sign || sign
|
|
1521
|
+
};
|
|
1522
|
+
this.binaryOps = {
|
|
1523
|
+
'+': add,
|
|
1524
|
+
'-': sub,
|
|
1525
|
+
'*': mul,
|
|
1526
|
+
'/': div,
|
|
1527
|
+
'%': mod,
|
|
1528
|
+
'^': Math.pow,
|
|
1529
|
+
'||': concat,
|
|
1530
|
+
'==': equal,
|
|
1531
|
+
'!=': notEqual,
|
|
1532
|
+
'>': greaterThan,
|
|
1533
|
+
'<': lessThan,
|
|
1534
|
+
'>=': greaterThanEqual,
|
|
1535
|
+
'<=': lessThanEqual,
|
|
1536
|
+
and: andOperator,
|
|
1537
|
+
or: orOperator,
|
|
1538
|
+
'in': inOperator,
|
|
1539
|
+
'=': setVar,
|
|
1540
|
+
'[': arrayIndex
|
|
1541
|
+
};
|
|
1542
|
+
this.ternaryOps = {
|
|
1543
|
+
'?': condition
|
|
1544
|
+
};
|
|
1545
|
+
this.functions = {
|
|
1546
|
+
random: random,
|
|
1547
|
+
fac: factorial,
|
|
1548
|
+
min: min,
|
|
1549
|
+
max: max,
|
|
1550
|
+
hypot: Math.hypot || hypot,
|
|
1551
|
+
pyt: Math.hypot || hypot,
|
|
1552
|
+
// backward compat
|
|
1553
|
+
pow: Math.pow,
|
|
1554
|
+
atan2: Math.atan2,
|
|
1555
|
+
'if': condition,
|
|
1556
|
+
gamma: gamma,
|
|
1557
|
+
roundTo: roundTo,
|
|
1558
|
+
map: arrayMap,
|
|
1559
|
+
fold: arrayFold,
|
|
1560
|
+
filter: arrayFilter,
|
|
1561
|
+
indexOf: stringOrArrayIndexOf,
|
|
1562
|
+
join: arrayJoin
|
|
1563
|
+
};
|
|
1564
|
+
this.consts = {
|
|
1565
|
+
E: Math.E,
|
|
1566
|
+
PI: Math.PI,
|
|
1567
|
+
'true': true,
|
|
1568
|
+
'false': false
|
|
1569
|
+
};
|
|
1570
|
+
}
|
|
1571
|
+
Parser.prototype.parse = function (expr) {
|
|
1572
|
+
var instr = [];
|
|
1573
|
+
var parserState = new ParserState(this, new TokenStream(this, expr), {
|
|
1574
|
+
allowMemberAccess: this.options.allowMemberAccess
|
|
1575
|
+
});
|
|
1576
|
+
parserState.parseExpression(instr);
|
|
1577
|
+
parserState.expect(TEOF, 'EOF');
|
|
1578
|
+
return new Expression(instr, this);
|
|
1579
|
+
};
|
|
1580
|
+
Parser.prototype.evaluate = function (expr, variables) {
|
|
1581
|
+
return this.parse(expr).evaluate(variables);
|
|
1582
|
+
};
|
|
1583
|
+
var sharedParser = new Parser();
|
|
1584
|
+
Parser.parse = function (expr) {
|
|
1585
|
+
return sharedParser.parse(expr);
|
|
1586
|
+
};
|
|
1587
|
+
Parser.evaluate = function (expr, variables) {
|
|
1588
|
+
return sharedParser.parse(expr).evaluate(variables);
|
|
1589
|
+
};
|
|
1590
|
+
var optionNameMap = {
|
|
1591
|
+
'+': 'add',
|
|
1592
|
+
'-': 'subtract',
|
|
1593
|
+
'*': 'multiply',
|
|
1594
|
+
'/': 'divide',
|
|
1595
|
+
'%': 'remainder',
|
|
1596
|
+
'^': 'power',
|
|
1597
|
+
'!': 'factorial',
|
|
1598
|
+
'<': 'comparison',
|
|
1599
|
+
'>': 'comparison',
|
|
1600
|
+
'<=': 'comparison',
|
|
1601
|
+
'>=': 'comparison',
|
|
1602
|
+
'==': 'comparison',
|
|
1603
|
+
'!=': 'comparison',
|
|
1604
|
+
'||': 'concatenate',
|
|
1605
|
+
'and': 'logical',
|
|
1606
|
+
'or': 'logical',
|
|
1607
|
+
'not': 'logical',
|
|
1608
|
+
'?': 'conditional',
|
|
1609
|
+
':': 'conditional',
|
|
1610
|
+
'=': 'assignment',
|
|
1611
|
+
'[': 'array',
|
|
1612
|
+
'()=': 'fndef'
|
|
1613
|
+
};
|
|
1614
|
+
function getOptionName(op) {
|
|
1615
|
+
return optionNameMap.hasOwnProperty(op) ? optionNameMap[op] : op;
|
|
1616
|
+
}
|
|
1617
|
+
Parser.prototype.isOperatorEnabled = function (op) {
|
|
1618
|
+
var optionName = getOptionName(op);
|
|
1619
|
+
var operators = this.options.operators || {};
|
|
1620
|
+
return !(optionName in operators) || !!operators[optionName];
|
|
1621
|
+
};
|
|
1622
|
+
|
|
1623
|
+
/*!
|
|
1624
|
+
Based on ndef.parser, by Raphael Graf(r@undefined.ch)
|
|
1625
|
+
http://www.undefined.ch/mparser/index.html
|
|
1626
|
+
|
|
1627
|
+
Ported to JavaScript and modified by Matthew Crumley (email@matthewcrumley.com, http://silentmatt.com/)
|
|
1628
|
+
|
|
1629
|
+
You are free to use and modify this code in anyway you find useful. Please leave this comment in the code
|
|
1630
|
+
to acknowledge its original source. If you feel like it, I enjoy hearing about projects that use my code,
|
|
1631
|
+
but don't feel like you have to let me know or ask permission.
|
|
1632
|
+
*/
|
|
1633
|
+
|
|
1634
|
+
// Backwards compatibility
|
|
1635
|
+
var index = {
|
|
1636
|
+
Parser: Parser,
|
|
1637
|
+
Expression: Expression
|
|
1638
|
+
};
|
|
1639
|
+
|
|
1640
|
+
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
|
|
1641
|
+
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
1642
|
+
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
1643
|
+
var parser = new Parser();
|
|
1644
|
+
function isPathBinding(value) {
|
|
1645
|
+
return _typeof(value) === "object" && value !== null && "path" in value && !("expr" in value);
|
|
1646
|
+
}
|
|
1647
|
+
function isExprBinding(value) {
|
|
1648
|
+
return _typeof(value) === "object" && value !== null && "expr" in value;
|
|
1649
|
+
}
|
|
1650
|
+
function resolveBinding(value, data) {
|
|
1651
|
+
var contextPath = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "/";
|
|
1652
|
+
if (isExprBinding(value)) {
|
|
1653
|
+
return resolveExprBinding(value, data, contextPath);
|
|
1654
|
+
}
|
|
1655
|
+
if (isPathBinding(value)) {
|
|
1656
|
+
return resolvePathBinding(value.path, data, contextPath);
|
|
1657
|
+
}
|
|
1658
|
+
return value;
|
|
1659
|
+
}
|
|
1660
|
+
function resolvePathBinding(path, data, contextPath) {
|
|
1661
|
+
var fullPath = path;
|
|
1662
|
+
if (!path.startsWith("/")) {
|
|
1663
|
+
if (!contextPath || contextPath === "/") {
|
|
1664
|
+
fullPath = "/".concat(path);
|
|
1665
|
+
} else {
|
|
1666
|
+
fullPath = "".concat(contextPath, "/").concat(path);
|
|
1667
|
+
}
|
|
1668
|
+
}
|
|
1669
|
+
fullPath = fullPath.replace(/\/+/g, "/");
|
|
1670
|
+
var segments = fullPath.split("/").filter(Boolean);
|
|
1671
|
+
var result = data;
|
|
1672
|
+
var _iterator = _createForOfIteratorHelper(segments),
|
|
1673
|
+
_step;
|
|
1674
|
+
try {
|
|
1675
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
1676
|
+
var segment = _step.value;
|
|
1677
|
+
if (result === null || result === void 0) {
|
|
1678
|
+
return void 0;
|
|
1679
|
+
}
|
|
1680
|
+
result = result[segment];
|
|
1681
|
+
}
|
|
1682
|
+
} catch (err) {
|
|
1683
|
+
_iterator.e(err);
|
|
1684
|
+
} finally {
|
|
1685
|
+
_iterator.f();
|
|
1686
|
+
}
|
|
1687
|
+
return result;
|
|
1688
|
+
}
|
|
1689
|
+
function resolveExprBinding(binding, data, contextPath) {
|
|
1690
|
+
var expr = binding.expr,
|
|
1691
|
+
_binding$vars = binding.vars,
|
|
1692
|
+
vars = _binding$vars === void 0 ? {} : _binding$vars,
|
|
1693
|
+
format = binding.format;
|
|
1694
|
+
var resolvedVars = {};
|
|
1695
|
+
for (var _i = 0, _Object$entries = Object.entries(vars); _i < _Object$entries.length; _i++) {
|
|
1696
|
+
var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
|
|
1697
|
+
key = _Object$entries$_i[0],
|
|
1698
|
+
varBinding = _Object$entries$_i[1];
|
|
1699
|
+
if (isPathBinding(varBinding)) {
|
|
1700
|
+
resolvedVars[key] = resolvePathBinding(varBinding.path, data, contextPath);
|
|
1701
|
+
} else if (isExprBinding(varBinding)) {
|
|
1702
|
+
resolvedVars[key] = resolveExprBinding(varBinding, data, contextPath);
|
|
1703
|
+
} else {
|
|
1704
|
+
resolvedVars[key] = varBinding;
|
|
1705
|
+
}
|
|
1706
|
+
}
|
|
1707
|
+
try {
|
|
1708
|
+
var result = evaluateExpression(expr, resolvedVars);
|
|
1709
|
+
if (format && typeof result === "number") {
|
|
1710
|
+
return formatValue(result, format);
|
|
1711
|
+
}
|
|
1712
|
+
return result;
|
|
1713
|
+
} catch (e) {
|
|
1714
|
+
console.warn("Expression evaluation error:", e);
|
|
1715
|
+
return void 0;
|
|
1716
|
+
}
|
|
1717
|
+
}
|
|
1718
|
+
function evaluateExpression(expr, vars) {
|
|
1719
|
+
var processedVars = {};
|
|
1720
|
+
var hasString = false;
|
|
1721
|
+
for (var _i2 = 0, _Object$entries2 = Object.entries(vars); _i2 < _Object$entries2.length; _i2++) {
|
|
1722
|
+
var _Object$entries2$_i = _slicedToArray(_Object$entries2[_i2], 2),
|
|
1723
|
+
key = _Object$entries2$_i[0],
|
|
1724
|
+
value = _Object$entries2$_i[1];
|
|
1725
|
+
if (Array.isArray(value)) {
|
|
1726
|
+
processedVars[key] = value.length;
|
|
1727
|
+
} else if (typeof value === "number") {
|
|
1728
|
+
processedVars[key] = value;
|
|
1729
|
+
} else if (typeof value === "string") {
|
|
1730
|
+
processedVars[key] = value;
|
|
1731
|
+
hasString = true;
|
|
1732
|
+
} else if (typeof value === "boolean") {
|
|
1733
|
+
processedVars[key] = value ? 1 : 0;
|
|
1734
|
+
} else if (value === null || value === void 0) {
|
|
1735
|
+
processedVars[key] = 0;
|
|
1736
|
+
} else {
|
|
1737
|
+
processedVars[key] = String(value);
|
|
1738
|
+
hasString = true;
|
|
1739
|
+
}
|
|
1740
|
+
}
|
|
1741
|
+
var hasStringLiteral = /"[^"]*"/.test(expr);
|
|
1742
|
+
if (hasString || hasStringLiteral) {
|
|
1743
|
+
return evaluateStringExpression(expr, processedVars);
|
|
1744
|
+
}
|
|
1745
|
+
var parsedExpr = parser.parse(expr);
|
|
1746
|
+
return parsedExpr.evaluate(processedVars);
|
|
1747
|
+
}
|
|
1748
|
+
function evaluateStringExpression(expr, vars) {
|
|
1749
|
+
var processedExpr = expr;
|
|
1750
|
+
for (var _i3 = 0, _Object$entries3 = Object.entries(vars); _i3 < _Object$entries3.length; _i3++) {
|
|
1751
|
+
var _Object$entries3$_i = _slicedToArray(_Object$entries3[_i3], 2),
|
|
1752
|
+
key = _Object$entries3$_i[0],
|
|
1753
|
+
value = _Object$entries3$_i[1];
|
|
1754
|
+
var regex = new RegExp("\\b".concat(key, "\\b"), "g");
|
|
1755
|
+
if (typeof value === "number") {
|
|
1756
|
+
processedExpr = processedExpr.replace(regex, String(value));
|
|
1757
|
+
} else if (typeof value === "string") {
|
|
1758
|
+
processedExpr = processedExpr.replace(regex, "\"".concat(value, "\""));
|
|
1759
|
+
}
|
|
1760
|
+
}
|
|
1761
|
+
var dangerousPatterns = [/\beval\b/i, /\bFunction\b/, /\bwindow\b/i, /\bdocument\b/i, /\bglobal\b/i, /\bprocess\b/i, /\brequire\b/i, /\bimport\b/i, /\bexport\b/i, /\b__proto__\b/, /\bconstructor\b/, /\bprototype\b/, /\bfetch\b/i, /\bXMLHttpRequest\b/i];
|
|
1762
|
+
for (var _i4 = 0, _dangerousPatterns = dangerousPatterns; _i4 < _dangerousPatterns.length; _i4++) {
|
|
1763
|
+
var pattern = _dangerousPatterns[_i4];
|
|
1764
|
+
if (pattern.test(processedExpr)) {
|
|
1765
|
+
throw new Error("Unsafe expression: contains forbidden pattern");
|
|
1766
|
+
}
|
|
1767
|
+
}
|
|
1768
|
+
return new Function("return (".concat(processedExpr, ")"))();
|
|
1769
|
+
}
|
|
1770
|
+
function formatValue(value, format) {
|
|
1771
|
+
switch (format) {
|
|
1772
|
+
case "currency":
|
|
1773
|
+
return "\xA5".concat(value.toFixed(2));
|
|
1774
|
+
case "currency-int":
|
|
1775
|
+
return "\xA5".concat(value);
|
|
1776
|
+
case "percent":
|
|
1777
|
+
return "".concat((value * 100).toFixed(0), "%");
|
|
1778
|
+
default:
|
|
1779
|
+
return String(value);
|
|
1780
|
+
}
|
|
1781
|
+
}
|
|
1782
|
+
|
|
1783
|
+
export { evaluateExpression, formatValue, isExprBinding, isPathBinding, resolveBinding, resolveExprBinding, resolvePathBinding };
|
|
1784
|
+
//# sourceMappingURL=binding.js.map
|