| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- /**
- * @license
- * Visual Blocks Language
- *
- * Copyright 2012 Google Inc.
- * https://blockly.googlecode.com/
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * @fileoverview Helper functions for generating OpenSCAD for blocks.
- * @author fraser@google.com (Neil Fraser)
- */
- 'use strict';
- goog.provide('Blockly.OpenSCAD');
- goog.require('Blockly.Generator');
- Blockly.OpenSCAD = new Blockly.Generator('OpenSCAD');
- /**
- * List of illegal variable names.
- * This is not intended to be a security feature. Blockly is 100% client-side,
- * so bypassing this list is trivial. This is intended to prevent users from
- * accidentally clobbering a built-in object or function.
- * @private
- */
- Blockly.OpenSCAD.addReservedWords(
- 'Blockly,' + // In case JS is evaled in the current window.
- // OpenSCAD reserved words
- 'break,delete,for,function,if,return,intersection_for,union,difference,intersection,module,' +
- 'rotate,translate,mirror,minkowski,scale,resize,hull,use,assign,color,linear_extrude,rotate_extrude,twist-rotate-extrude' +
- 'import,sphere,cylinder,cube,square,circle,torus,twistytorus,' +
- 'null,true,false,undef' +
- // https://developer.mozilla.org/en/DOM/window
- 'applicationCache,closed,Components,content,_content,controllers,crypto,defaultStatus,dialogArguments,directories,document,frameElement,frames,fullScreen,globalStorage,history,innerHeight,innerWidth,length,location,locationbar,localStorage,menubar,messageManager,mozAnimationStartTime,mozInnerScreenX,mozInnerScreenY,mozPaintCount,name,navigator,opener,outerHeight,outerWidth,pageXOffset,pageYOffset,parent,performance,personalbar,pkcs11,returnValue,screen,screenX,screenY,scrollbars,scrollMaxX,scrollMaxY,scrollX,scrollY,self,sessionStorage,sidebar,status,statusbar,toolbar,top,URL,window,' +
- 'addEventListener,alert,atob,back,blur,btoa,captureEvents,clearImmediate,clearInterval,clearTimeout,close,confirm,disableExternalCapture,dispatchEvent,dump,enableExternalCapture,escape,find,focus,forward,GeckoActiveXObject,getAttention,getAttentionWithCycleCount,getComputedStyle,getSelection,home,matchMedia,maximize,minimize,moveBy,moveTo,mozRequestAnimationFrame,open,openDialog,postMessage,print,prompt,QueryInterface,releaseEvents,removeEventListener,resizeBy,resizeTo,restore,routeEvent,scroll,scrollBy,scrollByLines,scrollByPages,scrollTo,setCursor,setImmediate,setInterval,setResizable,setTimeout,showModalDialog,sizeToContent,stop,unescape,updateCommands,XPCNativeWrapper,XPCSafeJSObjectWrapper,' +
- 'onabort,onbeforeunload,onblur,onchange,onclick,onclose,oncontextmenu,ondevicemotion,ondeviceorientation,ondragdrop,onerror,onfocus,onhashchange,onkeydown,onkeypress,onkeyup,onload,onmousedown,onmousemove,onmouseout,onmouseover,onmouseup,onmozbeforepaint,onpaint,onpopstate,onreset,onresize,onscroll,onselect,onsubmit,onunload,onpageshow,onpagehide,' +
- 'Image,Option,Worker,' +
- // https://developer.mozilla.org/en/Gecko_DOM_Reference
- 'Event,Range,File,FileReader,Blob,BlobBuilder,' +
- 'Attr,CDATASection,CharacterData,Comment,console,DocumentFragment,DocumentType,DomConfiguration,DOMError,DOMErrorHandler,DOMException,DOMImplementation,DOMImplementationList,DOMImplementationRegistry,DOMImplementationSource,DOMLocator,DOMObject,DOMString,DOMStringList,DOMTimeStamp,DOMUserData,Entity,EntityReference,MediaQueryList,MediaQueryListListener,NameList,NamedNodeMap,Node,NodeFilter,NodeIterator,NodeList,Notation,Plugin,PluginArray,ProcessingInstruction,SharedWorker,Text,TimeRanges,Treewalker,TypeInfo,UserDataHandler,Worker,WorkerGlobalScope,' +
- 'HTMLDocument,HTMLElement,HTMLAnchorElement,HTMLAppletElement,HTMLAudioElement,HTMLAreaElement,HTMLBaseElement,HTMLBaseFontElement,HTMLBodyElement,HTMLBRElement,HTMLButtonElement,HTMLCanvasElement,HTMLDirectoryElement,HTMLDivElement,HTMLDListElement,HTMLEmbedElement,HTMLFieldSetElement,HTMLFontElement,HTMLFormElement,HTMLFrameElement,HTMLFrameSetElement,HTMLHeadElement,HTMLHeadingElement,HTMLHtmlElement,HTMLHRElement,HTMLIFrameElement,HTMLImageElement,HTMLInputElement,HTMLKeygenElement,HTMLLabelElement,HTMLLIElement,HTMLLinkElement,HTMLMapElement,HTMLMenuElement,HTMLMetaElement,HTMLModElement,HTMLObjectElement,HTMLOListElement,HTMLOptGroupElement,HTMLOptionElement,HTMLOutputElement,HTMLParagraphElement,HTMLParamElement,HTMLPreElement,HTMLQuoteElement,HTMLScriptElement,HTMLSelectElement,HTMLSourceElement,HTMLSpanElement,HTMLStyleElement,HTMLTableElement,HTMLTableCaptionElement,HTMLTableCellElement,HTMLTableDataCellElement,HTMLTableHeaderCellElement,HTMLTableColElement,HTMLTableRowElement,HTMLTableSectionElement,HTMLTextAreaElement,HTMLTimeElement,HTMLTitleElement,HTMLTrackElement,HTMLUListElement,HTMLUnknownElement,HTMLVideoElement,' +
- 'HTMLCanvasElement,CanvasRenderingContext2D,CanvasGradient,CanvasPattern,TextMetrics,ImageData,CanvasPixelArray,HTMLAudioElement,HTMLVideoElement,NotifyAudioAvailableEvent,HTMLCollection,HTMLAllCollection,HTMLFormControlsCollection,HTMLOptionsCollection,HTMLPropertiesCollection,DOMTokenList,DOMSettableTokenList,DOMStringMap,RadioNodeList,' +
- 'SVGDocument,SVGElement,SVGAElement,SVGAltGlyphElement,SVGAltGlyphDefElement,SVGAltGlyphItemElement,SVGAnimationElement,SVGAnimateElement,SVGAnimateColorElement,SVGAnimateMotionElement,SVGAnimateTransformElement,SVGSetElement,SVGCircleElement,SVGClipPathElement,SVGColorProfileElement,SVGCursorElement,SVGDefsElement,SVGDescElement,SVGEllipseElement,SVGFilterElement,SVGFilterPrimitiveStandardAttributes,SVGFEBlendElement,SVGFEColorMatrixElement,SVGFEComponentTransferElement,SVGFECompositeElement,SVGFEConvolveMatrixElement,SVGFEDiffuseLightingElement,SVGFEDisplacementMapElement,SVGFEDistantLightElement,SVGFEFloodElement,SVGFEGaussianBlurElement,SVGFEImageElement,SVGFEMergeElement,SVGFEMergeNodeElement,SVGFEMorphologyElement,SVGFEOffsetElement,SVGFEPointLightElement,SVGFESpecularLightingElement,SVGFESpotLightElement,SVGFETileElement,SVGFETurbulenceElement,SVGComponentTransferFunctionElement,SVGFEFuncRElement,SVGFEFuncGElement,SVGFEFuncBElement,SVGFEFuncAElement,SVGFontElement,SVGFontFaceElement,SVGFontFaceFormatElement,SVGFontFaceNameElement,SVGFontFaceSrcElement,SVGFontFaceUriElement,SVGForeignObjectElement,SVGGElement,SVGGlyphElement,SVGGlyphRefElement,SVGGradientElement,SVGLinearGradientElement,SVGRadialGradientElement,SVGHKernElement,SVGImageElement,SVGLineElement,SVGMarkerElement,SVGMaskElement,SVGMetadataElement,SVGMissingGlyphElement,SVGMPathElement,SVGPathElement,SVGPatternElement,SVGPolylineElement,SVGPolygonElement,SVGRectElement,SVGScriptElement,SVGStopElement,SVGStyleElement,SVGSVGElement,SVGSwitchElement,SVGSymbolElement,SVGTextElement,SVGTextPathElement,SVGTitleElement,SVGTRefElement,SVGTSpanElement,SVGUseElement,SVGViewElement,SVGVKernElement,' +
- 'SVGAngle,SVGColor,SVGICCColor,SVGElementInstance,SVGElementInstanceList,SVGLength,SVGLengthList,SVGMatrix,SVGNumber,SVGNumberList,SVGPaint,SVGPoint,SVGPointList,SVGPreserveAspectRatio,SVGRect,SVGStringList,SVGTransform,SVGTransformList,' +
- 'SVGAnimatedAngle,SVGAnimatedBoolean,SVGAnimatedEnumeration,SVGAnimatedInteger,SVGAnimatedLength,SVGAnimatedLengthList,SVGAnimatedNumber,SVGAnimatedNumberList,SVGAnimatedPreserveAspectRatio,SVGAnimatedRect,SVGAnimatedString,SVGAnimatedTransformList,' +
- 'SVGPathSegList,SVGPathSeg,SVGPathSegArcAbs,SVGPathSegArcRel,SVGPathSegClosePath,SVGPathSegCurvetoCubicAbs,SVGPathSegCurvetoCubicRel,SVGPathSegCurvetoCubicSmoothAbs,SVGPathSegCurvetoCubicSmoothRel,SVGPathSegCurvetoQuadraticAbs,SVGPathSegCurvetoQuadraticRel,SVGPathSegCurvetoQuadraticSmoothAbs,SVGPathSegCurvetoQuadraticSmoothRel,SVGPathSegLinetoAbs,SVGPathSegLinetoHorizontalAbs,SVGPathSegLinetoHorizontalRel,SVGPathSegLinetoRel,SVGPathSegLinetoVerticalAbs,SVGPathSegLinetoVerticalRel,SVGPathSegMovetoAbs,SVGPathSegMovetoRel,ElementTimeControl,TimeEvent,SVGAnimatedPathData,' +
- 'SVGAnimatedPoints,SVGColorProfileRule,SVGCSSRule,SVGExternalResourcesRequired,SVGFitToViewBox,SVGLangSpace,SVGLocatable,SVGRenderingIntent,SVGStylable,SVGTests,SVGTextContentElement,SVGTextPositioningElement,SVGTransformable,SVGUnitTypes,SVGURIReference,SVGViewSpec,SVGZoomAndPan');
- /**
- * Order of operation ENUMs.
- * https://developer.mozilla.org/en/OpenSCAD/Reference/Operators/Operator_Precedence
- */
- Blockly.OpenSCAD.ORDER_ATOMIC = 0; // 0 "" ...
- Blockly.OpenSCAD.ORDER_MEMBER = 1; // . []
- Blockly.OpenSCAD.ORDER_NEW = 1; // new
- Blockly.OpenSCAD.ORDER_FUNCTION_CALL = 2; // ()
- Blockly.OpenSCAD.ORDER_INCREMENT = 3; // ++
- Blockly.OpenSCAD.ORDER_DECREMENT = 3; // --
- Blockly.OpenSCAD.ORDER_LOGICAL_NOT = 4; // !
- Blockly.OpenSCAD.ORDER_BITWISE_NOT = 4; // ~
- Blockly.OpenSCAD.ORDER_UNARY_PLUS = 4; // +
- Blockly.OpenSCAD.ORDER_UNARY_NEGATION = 4; // -
- Blockly.OpenSCAD.ORDER_TYPEOF = 4; // typeof
- Blockly.OpenSCAD.ORDER_VOID = 4; // void
- Blockly.OpenSCAD.ORDER_DELETE = 4; // delete
- Blockly.OpenSCAD.ORDER_MULTIPLICATION = 5; // *
- Blockly.OpenSCAD.ORDER_DIVISION = 5; // /
- Blockly.OpenSCAD.ORDER_MODULUS = 5; // %
- Blockly.OpenSCAD.ORDER_ADDITION = 6; // +
- Blockly.OpenSCAD.ORDER_SUBTRACTION = 6; // -
- Blockly.OpenSCAD.ORDER_BITWISE_SHIFT = 7; // << >> >>>
- Blockly.OpenSCAD.ORDER_RELATIONAL = 8; // < <= > >=
- Blockly.OpenSCAD.ORDER_IN = 8; // in
- Blockly.OpenSCAD.ORDER_INSTANCEOF = 8; // instanceof
- Blockly.OpenSCAD.ORDER_EQUALITY = 9; // == != === !==
- Blockly.OpenSCAD.ORDER_BITWISE_AND = 10; // &
- Blockly.OpenSCAD.ORDER_BITWISE_XOR = 11; // ^
- Blockly.OpenSCAD.ORDER_BITWISE_OR = 12; // |
- Blockly.OpenSCAD.ORDER_LOGICAL_AND = 13; // &&
- Blockly.OpenSCAD.ORDER_LOGICAL_OR = 14; // ||
- Blockly.OpenSCAD.ORDER_CONDITIONAL = 15; // ?:
- Blockly.OpenSCAD.ORDER_ASSIGNMENT = 16; // = += -= *= /= %= <<= >>= ...
- Blockly.OpenSCAD.ORDER_COMMA = 17; // ,
- Blockly.OpenSCAD.ORDER_NONE = 99; // (...)
- /**
- * Arbitrary code to inject into locations that risk causing infinite loops.
- * Any instances of '%1' will be replaced by the block ID that failed.
- * E.g. ' checkTimeout(%1);\n'
- * @type ?string
- */
- Blockly.OpenSCAD.INFINITE_LOOP_TRAP = null;
- /**
- * Initialise the database of variable names.
- * @param {!Blockly.Workspace} workspace Workspace to generate code from.
- */
- Blockly.OpenSCAD.init = function(workspace) {
- // Create a dictionary of definitions to be printed before the code.
- Blockly.OpenSCAD.definitions_ = Object.create(null);
- // Create a dictionary mapping desired function names in definitions_
- // to actual function names (to avoid collisions with user functions).
- Blockly.OpenSCAD.functionNames_ = Object.create(null);
- if (!Blockly.OpenSCAD.variableDB_) {
- Blockly.OpenSCAD.variableDB_ =
- new Blockly.Names(Blockly.OpenSCAD.RESERVED_WORDS_);
- } else {
- Blockly.OpenSCAD.variableDB_.reset();
- }
- var defvars = [];
- var variables = Blockly.Variables.allVariables(workspace);
- // for (var x = 0; x < variables.length; x++) {
- // defvars[x] = 'var ' +
- // Blockly.OpenSCAD.variableDB_.getName(variables[x],
- // Blockly.Variables.NAME_TYPE) + ';';
- // }
- Blockly.OpenSCAD.definitions_['variables'] = defvars.join('\n');
- };
- /**
- * Prepend the generated code with the variable definitions.
- * @param {string} code Generated code.
- * @return {string} Completed code.
- */
- Blockly.OpenSCAD.finish = function(code) {
- // Convert the definitions dictionary into a list.
- var definitions = [];
- for (var name in Blockly.OpenSCAD.definitions_) {
- definitions.push(Blockly.OpenSCAD.definitions_[name]);
- }
- return '//!OpenSCAD\n\n' + definitions.join('\n') + '\n\n\n' + code;
- //return definitions.join('\n\n') + '\n\n\n' + code;
- };
- /**
- * Naked values are top-level blocks with outputs that aren't plugged into
- * anything. A trailing semicolon is needed to make this legal.
- * @param {string} line Line of generated code.
- * @return {string} Legal line of code.
- */
- Blockly.OpenSCAD.scrubNakedValue = function(line) {
- return line + ';\n';
- };
- /**
- * Encode a string as a properly escaped OpenSCAD string, complete with
- * quotes.
- * @param {string} string Text to encode.
- * @return {string} OpenSCAD string.
- * @private
- */
- Blockly.OpenSCAD.quote_ = function(string) {
- // TODO: This is a quick hack. Replace with goog.string.quote
- string = string.replace(/\\/g, '\\\\')
- .replace(/\n/g, '\\\n')
- .replace(/"/g, '\\\"');
- return '"' + string + '"';
- };
- /**
- * Common tasks for generating OpenSCAD from blocks.
- * Handles comments for the specified block and any connected value blocks.
- * Calls any statements following this block.
- * @param {!Blockly.Block} block The current block.
- * @param {string} code The OpenSCAD code created for this block.
- * @return {string} OpenSCAD code with comments and subsequent blocks added.
- * @this {Blockly.CodeGenerator}
- * @private
- */
- Blockly.OpenSCAD.scrub_ = function(block, code) {
- if (code === null) {
- // Block has handled code generation itself.
- return '';
- }
- var commentCode = '';
- // Only collect comments for blocks that aren't inline.
- if (!block.outputConnection || !block.outputConnection.targetConnection) {
- // Collect comment for this block.
- var comment = block.getCommentText();
- if (comment) {
- commentCode += this.prefixLines(comment, '// ') + '\n';
- }
- // Collect comments for all value arguments.
- // Don't collect comments for nested statements.
- for (var x = 0; x < block.inputList.length; x++) {
- if (block.inputList[x].type == Blockly.INPUT_VALUE) {
- var childBlock = block.inputList[x].connection.targetBlock();
- if (childBlock) {
- var comment = this.allNestedComments(childBlock);
- if (comment) {
- commentCode += this.prefixLines(comment, '// ');
- }
- }
- }
- }
- }
- var nextBlock = block.nextConnection && block.nextConnection.targetBlock();
- var nextCode = this.blockToCode(nextBlock);
- return commentCode + code + nextCode;
- };
|