public JsDirectivePrologue(string value, JsContext context, JsParser parser) : base(value, JsPrimitiveType.String, context, parser) { // this is a "use strict" directive if the source context is EXACTLY "use strict" // don't consider the quotes so it can be " or ' delimiters UseStrict = string.CompareOrdinal(Context.Code, 1, "use strict", 0, 10) == 0; }
private JsReorderScopeVisitor(JsParser parser) { // save the mods we care about m_moveVarStatements = parser.Settings.ReorderScopeDeclarations && parser.Settings.IsModificationAllowed(JsTreeModifications.CombineVarStatementsToTopOfScope); m_moveFunctionDecls = parser.Settings.ReorderScopeDeclarations && parser.Settings.IsModificationAllowed(JsTreeModifications.MoveFunctionToTopOfScope); m_combineAdjacentVars = parser.Settings.IsModificationAllowed(JsTreeModifications.CombineVarStatements); }
private void Form1_Shown(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); DialogResult result; do { result = ofd.ShowDialog(); } while (result != DialogResult.OK || ofd.FileName == null); string fileName = ofd.FileName; FileInfo snakewareLogo = new FileInfo(fileName); MessageBox.Show("Bytes before: " + snakewareLogo.Length); ImageOptimizer optimizer = new ImageOptimizer(); optimizer.LosslessCompress(snakewareLogo); snakewareLogo.Refresh(); MessageBox.Show("Bytes after: " + snakewareLogo.Length); //Compressor return; AntlrFileStream inputStream = new AntlrFileStream(fileName); IParseTree tree = null; IVocabulary names = null; string typeS = fileName.Substring(fileName.LastIndexOf('.') + 1); switch (typeS.ToUpper()) { case "CSS": tree = new CssParser(new CommonTokenStream(new CssLexer(inputStream))).stylesheet(); names = CssParser.DefaultVocabulary; break; case "HTML": tree = new HtmlParser(new CommonTokenStream(new HtmlLexer(inputStream))).htmlDocument(); names = HtmlParser.DefaultVocabulary; break; case "JS": tree = new JsParser(new CommonTokenStream(new JsLexer(inputStream))).program(); names = JsParser.DefaultVocabulary; break; } var node = treeView.Nodes.Add(ToText(tree, names)); RecursFn(tree, node, (i) => ToText(i, names)); /*var d = new CommonTokenStream(new HtmlLexer(inputStream)); * names = HtmlParser.DefaultVocabulary; * d.Fill(); * foreach(var item in d.GetTokens()) { * treeView.Nodes.Add(names.GetDisplayName(item.Type) + " --- " + item.ToString()); * }*/ }
protected JsAstNode(JsContext context, JsParser parser) { Parser = parser; if (context != null) { Context = context; } else { // generate a bogus context Context = new JsContext(parser); } }
public JsImportantComment(JsContext context, JsParser parser) : base(context, parser) { if (parser != null && parser.Settings.OutputMode == MinifierOutputMode.SingleLine) { // if we are in single-line mode, we want to replace all CRLF pairs // with just the LF to save output bytes. Comment = Context.Code.Replace("\r\n", "\n"); } else { // multi-line mode, just leave it as-is Comment = Context.Code; } }
public virtual ITokenSource Edit(ITokenSource tokens) { CommonTokenStream s = new CommonTokenStream(tokens); JsParser parser = new JsParser(s); s.Fill(); var program = parser.program(); visitor.Visit(program, s.GetTokens()); var source = new ListTokenSource(visitor.GetResult()); source.TokenFactory = tokens.TokenFactory; return(source); }
public ITokenSource Edit(ITokenSource tokens) { CommonTokenStream s = new CommonTokenStream(tokens); JsParser parser = new JsParser(s); s.Fill(); IVisitorTree v = new JsVisitorChangeLiteralString(factoryNames, tokens.TokenFactory); var program = parser.program(); v.Visit(program, s.GetTokens()); var source = new ListTokenSource(v.GetResult()); source.TokenFactory = tokens.TokenFactory; return(source); }
public JsParameterDeclaration(JsContext context, JsParser parser) : base(context, parser) { }
public JsFunctionObject(JsContext functionContext, JsParser parser) : base(functionContext, parser) { }
public JsThisLiteral(JsContext context, JsParser parser) : base(context, parser) { }
public JsConditionalCompilationElse(JsContext context, JsParser parser) : base(context, parser) { }
public JsGroupingOperator(JsContext context, JsParser parser) : base(context, parser) { }
protected JsConditionalCompilationStatement(JsContext context, JsParser parser) : base(context, parser) { }
public JsVar(JsContext context, JsParser parser) : base(context, parser) { }
public JsContinueNode(JsContext context, JsParser parser) : base(context, parser) { }
public JsDebuggerNode(JsContext context, JsParser parser) : base(context, parser) { }
public JsConditionalCompilationEnd(JsContext context, JsParser parser) : base(context, parser) { }
public static void Apply(JsBlock block, JsParser parser) { // create a new instance of the visitor and apply it to the block var visitor = new JsReorderScopeVisitor(parser); block.Accept(visitor); // if there were any module directive prologues we need to promote, do them first var insertAt = 0; if (visitor.m_moduleDirectives != null) { foreach (var directivePrologue in visitor.m_moduleDirectives) { insertAt = RelocateDirectivePrologue(block, insertAt, directivePrologue); } } // Make sure that we skip over any remaining comments and directive prologues. // we do NOT want to insert anything between the start of the scope and any directive prologues. while (insertAt < block.Count && (block[insertAt] is JsDirectivePrologue || block[insertAt] is JsImportantComment)) { ++insertAt; } // first, we want to move all function declarations to the top of this block if (visitor.m_functionDeclarations != null) { foreach (var funcDecl in visitor.m_functionDeclarations) { insertAt = RelocateFunction(block, insertAt, funcDecl); } } // special case: if there is only one var statement in the entire scope, // then just leave it alone because we will only add bytes by moving it around, // or be byte-neutral at best (no initializers and not in a for-statement). if (visitor.m_varStatements != null && visitor.m_varStatements.Count > 1) { // then we want to move all variable declarations after to the top (after the functions) foreach (var varStatement in visitor.m_varStatements) { insertAt = RelocateVar(block, insertAt, varStatement); } } // then we want to do the same thing for all child functions (declarations AND other) if (visitor.m_functionDeclarations != null) { foreach (var funcDecl in visitor.m_functionDeclarations) { Apply(funcDecl.Body, parser); } } if (visitor.m_functionExpressions != null) { foreach (var funcExpr in visitor.m_functionExpressions) { Apply(funcExpr.Body, parser); } } }
public JsDocumentContext(JsParser parser) { m_parser = parser; IsGenerated = true; Source = "[generated code]"; }
public JsObjectLiteral(JsContext context, JsParser parser) : base(context, parser) { }
public JsDocumentContext(JsParser parser, string source) { m_parser = parser; Source = source; }
public JsSwitchCase(JsContext context, JsParser parser) : base(context, parser) { }
public JsLogicalNot(JsAstNode node, JsParser parser) { m_expression = node; m_parser = parser; }
public JsRegExpLiteral(JsContext context, JsParser parser) : base(context, parser) { }
public static void Apply(JsAstNode node, JsParser parser) { var logicalNot = new JsLogicalNot(node, parser); logicalNot.Apply(); }
public JsDoWhile(JsContext context, JsParser parser) : base(context, parser) { }
public JsMember(JsContext context, JsParser parser) : base(context, parser) { }
public JsBreak(JsContext context, JsParser parser) : base(context, parser) { }
public JsContext(JsParser parser) : this(new JsDocumentContext(parser)) { }
public JsConstantWrapperPP(JsContext context, JsParser parser) : base(context, parser) { }
public JsReturnNode(JsContext context, JsParser parser) : base(context, parser) { }
public JsGetterSetter(String identifier, bool isGetter, JsContext context, JsParser parser) : base(identifier, JsPrimitiveType.String, context, parser) { IsGetter = isGetter; }
public JsBlock(JsContext context, JsParser parser) : base(context, parser) { m_list = new List <JsAstNode>(); }