1
1
mirror of https://github.com/github/semantic.git synced 2024-11-24 08:54:07 +03:00

Merge branch 'master' into go-assignment

This commit is contained in:
Rick Winfrey 2017-10-10 11:44:14 -07:00
commit eb591f12aa
123 changed files with 917 additions and 2637 deletions

View File

@ -14,9 +14,6 @@ error "Avoid return" =
return ==> pure
where note = "return is obsolete as of GHC 7.10"
error "use pure" = free . Pure ==> pure
error "use wrap" = free . Free ==> wrap
error "use extract" = termAnnotation . unTerm ==> extract
error "use unwrap" = termOut . unTerm ==> unwrap

View File

@ -56,13 +56,11 @@ library
, Language.Go.Syntax
, Language.JSON.Grammar
, Language.JSON.Syntax
, Language.Ruby
, Language.Ruby.Grammar
, Language.Ruby.Syntax
, Language.TypeScript
, Language.TypeScript.Grammar
, Language.TypeScript.Syntax
, Language.Python
, Language.Python.Grammar
, Language.Python.Syntax
, Parser
@ -75,7 +73,9 @@ library
, RWS
, Semantic
, Semantic.Log
, Semantic.Stat
, Semantic.Task
, Semantic.Queue
, Semantic.Util
, SemanticCmdLine
, SES
@ -102,6 +102,8 @@ library
, mersenne-random-pure64
, MonadRandom
, mtl
, network
, network-uri
, optparse-applicative
, parallel
, parsers
@ -152,6 +154,7 @@ test-suite test
, DiffSpec
, SemanticSpec
, SemanticCmdLineSpec
, Semantic.StatSpec
, InterpreterSpec
, PatchOutputSpec
, SES.Spec
@ -178,6 +181,7 @@ test-suite test
, HUnit
, leancheck
, mtl
, network
, containers
, recursion-schemes >= 4.1
, semantic-diff

View File

@ -1,4 +1,4 @@
{-# LANGUAGE DeriveAnyClass, TypeOperators #-}
{-# LANGUAGE DeriveAnyClass, GADTs, TypeOperators #-}
module Data.Syntax where
import Algorithm hiding (Empty)
@ -15,6 +15,7 @@ import Data.Functor.Classes.Eq.Generic
import Data.Functor.Classes.Ord.Generic
import Data.Functor.Classes.Show.Generic
import Data.Mergeable
import Data.Range
import Data.Record
import Data.Semigroup
import Data.Span
@ -46,7 +47,8 @@ makeTerm1' f = case toList f of
-- | Construct an empty term at the current position.
emptyTerm :: (HasCallStack, Empty :< fs, Apply Foldable fs) => Assignment.Assignment ast grammar (Term (Union fs) (Record Assignment.Location))
emptyTerm = makeTerm <$> Assignment.location <*> pure Empty
emptyTerm = makeTerm . startLocation <$> Assignment.location <*> pure Empty
where startLocation ann = Range (start (getField ann)) (start (getField ann)) :. Span (spanStart (getField ann)) (spanStart (getField ann)) :. Nil
-- | Catch assignment errors into an error term.
handleError :: (HasCallStack, Error :< fs, Enum grammar, Eq1 ast, Ix grammar, Show grammar, Apply Foldable fs) => Assignment.Assignment ast grammar (Term (Union fs) (Record Assignment.Location)) -> Assignment.Assignment ast grammar (Term (Union fs) (Record Assignment.Location))

View File

@ -7,6 +7,7 @@ module Data.Syntax.Algebra
, identifierAlgebra
, syntaxIdentifierAlgebra
, cyclomaticComplexityAlgebra
, ConstructorName(..)
, ConstructorLabel(..)
, constructorNameAndConstantFields
, constructorLabel

View File

@ -1 +0,0 @@
module Language.Python where

View File

@ -39,8 +39,6 @@ type Syntax =
, Declaration.Decorator
, Declaration.Function
, Declaration.Import
-- NB: ToC rendering requires Methods in the union.
, Declaration.Method
, Declaration.Variable
, Expression.Arithmetic
, Expression.Boolean

View File

@ -1,177 +0,0 @@
{-# LANGUAGE DataKinds #-}
module Language.Ruby where
import Data.Foldable (toList)
import Data.List (partition)
import Data.Record
import Data.Semigroup
import Data.Source
import Data.Term
import Data.Text (Text)
import Info
import Language
import qualified Syntax as S
termAssignment
:: Source -- ^ The source of the term.
-> Category -- ^ The category for the term.
-> [ Term S.Syntax (Record DefaultFields) ] -- ^ The child nodes of the term.
-> Maybe (S.Syntax (Term S.Syntax (Record DefaultFields))) -- ^ The resulting term, in Maybe.
termAssignment _ category children
= case (category, children) of
(ArgumentPair, [ k, v ] ) -> Just $ S.Pair k v
(KeywordParameter, [ k, v ] ) -> Just $ S.Pair k v
-- NB: ("keyword_parameter", k) is a required keyword parameter, e.g.:
-- def foo(name:); end
-- Let it fall through to generate an Indexed syntax.
(OptionalParameter, [ k, v ] ) -> Just $ S.Pair k v
(AnonymousFunction, first : rest)
| null rest -> Just $ S.AnonymousFunction [] [first]
| otherwise -> Just $ S.AnonymousFunction (toList (unwrap first)) rest
(ArrayLiteral, _ ) -> Just $ S.Array Nothing children
(Assignment, [ identifier, value ]) -> Just $ S.Assignment identifier value
(Begin, _ ) -> Just $ case partition (\x -> Info.category (extract x) == Rescue) children of
(rescues, rest) -> case partition (\x -> Info.category (extract x) == Ensure || Info.category (extract x) == Else) rest of
(ensureElse, body) -> case ensureElse of
[ elseBlock, ensure ]
| Else <- Info.category (extract elseBlock)
, Ensure <- Info.category (extract ensure) -> S.Try body rescues (Just elseBlock) (Just ensure)
[ ensure, elseBlock ]
| Ensure <- Info.category (extract ensure)
, Else <- Info.category (extract elseBlock) -> S.Try body rescues (Just elseBlock) (Just ensure)
[ elseBlock ] | Else <- Info.category (extract elseBlock) -> S.Try body rescues (Just elseBlock) Nothing
[ ensure ] | Ensure <- Info.category (extract ensure) -> S.Try body rescues Nothing (Just ensure)
_ -> S.Try body rescues Nothing Nothing
(Class, constant : superclass : body)
| Superclass <- Info.category (extract superclass)
-> Just $ S.Class constant [superclass] body
(Class, constant : rest) -> Just $ S.Class constant [] rest
(SingletonClass, identifier : rest) -> Just $ S.Class identifier [] rest
(Case, _) -> Just $ uncurry S.Switch (break ((== When) . Info.category . extract) children)
(When, expr : body) -> Just $ S.Case expr body
(Ternary, condition : cases) -> Just $ S.Ternary condition cases
(MethodCall, fn : args)
| MemberAccess <- Info.category (extract fn)
, [target, method] <- toList (unwrap fn)
-> Just $ S.MethodCall target method [] (toList . unwrap =<< args)
| otherwise
-> Just $ S.FunctionCall fn [] (toList . unwrap =<< args)
(Object, _ ) -> Just . S.Object Nothing $ foldMap toTuple children
(Modifier If, [ lhs, condition ]) -> Just $ S.If condition [lhs]
(Modifier Unless, [lhs, rhs]) -> Just $ S.If (termIn (setCategory (extract rhs) Negate) (S.Negate rhs)) [lhs]
(Unless, expr : rest) -> Just $ S.If (termIn (setCategory (extract expr) Negate) (S.Negate expr)) rest
(Modifier Until, [ lhs, rhs ]) -> Just $ S.While (termIn (setCategory (extract rhs) Negate) (S.Negate rhs)) [lhs]
(Until, expr : rest) -> Just $ S.While (termIn (setCategory (extract expr) Negate) (S.Negate expr)) rest
(Elsif, condition : body ) -> Just $ S.If condition body
(SubscriptAccess, [ base, element ]) -> Just $ S.SubscriptAccess base element
(For, lhs : expr : rest ) -> Just $ S.For [lhs, expr] rest
(OperatorAssignment, [ identifier, value ]) -> Just $ S.OperatorAssignment identifier value
(MemberAccess, [ base, property ]) -> Just $ S.MemberAccess base property
(SingletonMethod, expr : methodName : rest)
| params : body <- rest
, Params <- Info.category (extract params)
-> Just $ S.Method [] methodName (Just expr) [params] body
| Identifier <- Info.category (extract methodName)
-> Just $ S.Method [] methodName (Just expr) [] rest
(Method, identifier : rest)
| params : body <- rest
, Params <- Info.category (extract params)
-> Just $ S.Method [] identifier Nothing [params] body
| otherwise
-> Just $ S.Method [] identifier Nothing [] rest
(Module, constant : body ) -> Just $ S.Module constant body
(Modifier Rescue, [lhs, rhs] ) -> Just $ S.Rescue [lhs] [rhs]
(Rescue, exceptions : exceptionVar : rest)
| RescueArgs <- Info.category (extract exceptions)
, RescuedException <- Info.category (extract exceptionVar)
-> Just $ S.Rescue (toList (unwrap exceptions) <> [exceptionVar]) rest
(Rescue, exceptionVar : rest)
| RescuedException <- Info.category (extract exceptionVar)
-> Just $ S.Rescue [exceptionVar] rest
(Rescue, exceptions : body)
| RescueArgs <- Info.category (extract exceptions)
-> Just $ S.Rescue (toList (unwrap exceptions)) body
(Rescue, body) -> Just $ S.Rescue [] body
(Modifier While, [ lhs, condition ]) -> Just $ S.While condition [lhs]
_ | category `elem` [ BeginBlock, EndBlock ] -> Just $ S.BlockStatement children
_ -> Nothing
categoryForRubyName :: Text -> Category
categoryForRubyName name = case name of
"argument_list_with_parens" -> Args
"argument_list" -> Args
"argument_pair" -> ArgumentPair
"array" -> ArrayLiteral
"assignment" -> Assignment
"begin_block" -> BeginBlock
"begin" -> Begin
"binary" -> Binary
"block" -> ExpressionStatements
"block_parameter" -> BlockParameter
"block_parameters" -> Params
"boolean" -> Boolean
"call" -> MemberAccess
"case" -> Case
"class" -> Class
"comment" -> Comment
"conditional" -> Ternary
"constant" -> Constant
"element_reference" -> SubscriptAccess
"else" -> Else
"elsif" -> Elsif
"empty_statement" -> Empty
"end_block" -> EndBlock
"ensure" -> Ensure
"exception_variable" -> RescuedException
"exceptions" -> RescueArgs
"false" -> Boolean
"float" -> NumberLiteral
"for" -> For
"hash_splat_parameter" -> HashSplatParameter
"hash" -> Object
"identifier" -> Identifier
"if_modifier" -> Modifier If
"if" -> If
"instance_variable" -> Identifier
"integer" -> IntegerLiteral
"interpolation" -> Interpolation
"keyword_parameter" -> KeywordParameter
"lambda_parameters" -> Params
"lambda" -> AnonymousFunction
"left_assignment_list" -> Args
"method_call" -> MethodCall
"method_parameters" -> Params
"method" -> Method
"module" -> Module
"nil" -> Identifier
"operator_assignment" -> OperatorAssignment
"optional_parameter" -> OptionalParameter
"pair" -> Pair
"pattern" -> Args
"program" -> Program
"range" -> RangeExpression
"regex" -> Regex
"rescue_modifier" -> Modifier Rescue
"rescue" -> Rescue
"rest_assignment" -> SplatParameter
"return" -> Return
"scope_resolution" -> ScopeOperator
"self" -> Identifier
"singleton_class" -> SingletonClass
"singleton_method" -> SingletonMethod
"splat_parameter" -> SplatParameter
"string" -> StringLiteral
"subshell" -> Subshell
"superclass" -> Superclass
"symbol" -> SymbolLiteral
"true" -> Boolean
"unary" -> Unary
"unless_modifier" -> Modifier Unless
"unless" -> Unless
"until_modifier" -> Modifier Until
"until" -> Until
"when" -> When
"while_modifier" -> Modifier While
"while" -> While
"yield" -> Yield
s -> Other s

View File

@ -235,11 +235,10 @@ singletonMethod = makeTerm <$> symbol SingletonMethod <*> children (Declaration.
where params = symbol MethodParameters *> children (many parameter) <|> pure []
lambda :: Assignment
lambda = symbol Lambda >>= \ loc -> children $ do
name <- makeTerm loc <$> (Syntax.Empty <$ source)
params <- (symbol BlockParameters <|> symbol LambdaParameters) *> children (many parameter) <|> pure []
body <- expressions
pure $ makeTerm loc (Declaration.Function [] name params body)
lambda = makeTerm <$> symbol Lambda <*> children (
Declaration.Function [] <$> emptyTerm
<*> ((symbol BlockParameters <|> symbol LambdaParameters) *> children (many parameter) <|> pure [])
<*> expressions)
block :: Assignment
block = makeTerm <$> symbol DoBlock <*> children (Declaration.Function <$> pure [] <*> emptyTerm <*> params <*> expressions)

View File

@ -171,6 +171,7 @@ type Syntax = '[
, Language.TypeScript.Syntax.This
, Language.TypeScript.Syntax.Update
, Language.TypeScript.Syntax.ComputedPropertyName
, Language.TypeScript.Syntax.Decorator
, []
]
@ -269,6 +270,13 @@ instance Eq1 Annotation where liftEq = genericLiftEq
instance Ord1 Annotation where liftCompare = genericLiftCompare
instance Show1 Annotation where liftShowsPrec = genericLiftShowsPrec
newtype Decorator a = Decorator { _decoratorTerm :: a }
deriving (Diffable, Eq, Foldable, Functor, GAlign, Generic1, Mergeable, Ord, Show, Traversable)
instance Eq1 Decorator where liftEq = genericLiftEq
instance Ord1 Decorator where liftCompare = genericLiftCompare
instance Show1 Decorator where liftShowsPrec = genericLiftShowsPrec
newtype ComputedPropertyName a = ComputedPropertyName a
deriving (Diffable, Eq, Foldable, Functor, GAlign, Generic1, Mergeable, Ord, Show, Traversable)
@ -659,7 +667,6 @@ expression = term (handleError everything)
importAlias',
internalModule,
super,
abstractClass,
object,
array,
jsxElement,
@ -728,7 +735,7 @@ ternaryExpression :: Assignment
ternaryExpression = makeTerm <$> symbol Grammar.TernaryExpression <*> children (Statement.If <$> expression <*> expression <*> expression)
memberExpression :: Assignment
memberExpression = makeTerm <$> symbol Grammar.MemberExpression <*> children (Expression.MemberAccess <$> postContextualize comment expression <*> propertyIdentifier)
memberExpression = makeTerm <$> (symbol Grammar.MemberExpression <|> symbol Grammar.MemberExpression') <*> children (Expression.MemberAccess <$> postContextualize comment expression <*> propertyIdentifier)
newExpression :: Assignment
newExpression = makeTerm <$> symbol Grammar.NewExpression <*> children (Expression.New <$> expression)
@ -800,8 +807,8 @@ identifier :: Assignment
identifier = (makeTerm <$> symbol Identifier' <*> (Syntax.Identifier <$> source)) <|> (makeTerm <$> symbol Identifier <*> (Syntax.Identifier <$> source))
class' :: Assignment
class' = makeClass <$> symbol Class <*> children ((,,,) <$> identifier <*> ((symbol TypeParameters *> children (many (term typeParameter'))) <|> pure []) <*> (classHeritage' <|> pure []) <*> classBodyStatements)
where makeClass loc (expression, typeParams, classHeritage, statements) = makeTerm loc (Declaration.Class typeParams expression classHeritage statements)
class' = makeClass <$> symbol Class <*> children ((,,,,) <$> many (term decorator) <*> identifier <*> ((symbol TypeParameters *> children (many (term typeParameter'))) <|> pure []) <*> (classHeritage' <|> pure []) <*> classBodyStatements)
where makeClass loc (decorators, expression, typeParams, classHeritage, statements) = makeTerm loc (Declaration.Class (decorators ++ typeParams) expression classHeritage statements)
object :: Assignment
object = (makeTerm <$> (symbol Object <|> symbol ObjectPattern) <*> children (Literal.Hash <$> many (term ((pair <|> spreadElement <|> methodDefinition <|> assignmentPattern <|> shorthandPropertyIdentifier)))))
@ -878,7 +885,10 @@ methodSignature = makeMethodSignature <$> symbol Grammar.MethodSignature <*> chi
where makeMethodSignature loc (modifier, readonly, propertyName, (typeParams, params, annotation)) = makeTerm loc (Language.TypeScript.Syntax.MethodSignature [modifier, readonly, typeParams, annotation] propertyName params)
formalParameters :: HasCallStack => Assignment.Assignment [] Grammar [Term]
formalParameters = symbol FormalParameters *> children (many (term parameter))
formalParameters = symbol FormalParameters *> children (concat <$> many ((\as b -> as ++ [b]) <$> many (term decorator) <*> term parameter))
decorator :: Assignment
decorator = makeTerm <$> symbol Grammar.Decorator <*> children (Language.TypeScript.Syntax.Decorator <$> (identifier <|> memberExpression <|> callExpression))
typeParameters :: Assignment
typeParameters = makeTerm <$> symbol TypeParameters <*> children (Type.TypeParameters <$> many (term typeParameter'))
@ -973,7 +983,7 @@ statementBlock :: Assignment
statementBlock = makeTerm <$> symbol StatementBlock <*> children (many statement)
classBodyStatements :: HasCallStack => Assignment.Assignment [] Grammar [Term]
classBodyStatements = symbol ClassBody *> children (many (term (methodDefinition <|> publicFieldDefinition <|> methodSignature <|> indexSignature)))
classBodyStatements = symbol ClassBody *> children (concat <$> many ((\as b -> as ++ [b]) <$> many (term decorator) <*> term (methodDefinition <|> publicFieldDefinition <|> methodSignature <|> indexSignature)))
publicFieldDefinition :: Assignment
publicFieldDefinition = makeField <$> symbol Grammar.PublicFieldDefinition <*> children ((,,,,) <$> (accessibilityModifier' <|> emptyTerm) <*> (readonly' <|> emptyTerm) <*> propertyName <*> (typeAnnotation' <|> emptyTerm) <*> (expression <|> emptyTerm))
@ -1063,6 +1073,7 @@ declaration = everything
function,
internalModule,
ambientFunction,
abstractClass,
class',
module',
variableDeclaration,
@ -1093,7 +1104,7 @@ ambientDeclaration :: Assignment
ambientDeclaration = makeTerm <$> symbol Grammar.AmbientDeclaration <*> children (Language.TypeScript.Syntax.AmbientDeclaration <$> choice [declaration, statementBlock])
exportStatement :: Assignment
exportStatement = makeTerm <$> symbol Grammar.ExportStatement <*> children (Language.TypeScript.Syntax.Export <$> (((\a b -> [a, b]) <$> exportClause <*> fromClause) <|> (pure <$> (fromClause <|> exportClause <|> declaration <|> expression <|> identifier <|> importAlias'))))
exportStatement = makeTerm <$> symbol Grammar.ExportStatement <*> children (Language.TypeScript.Syntax.Export <$> (((\a b -> [a, b]) <$> exportClause <*> fromClause) <|> ((++) <$> many (term decorator) <*> (pure <$> (fromClause <|> exportClause <|> declaration <|> expression <|> identifier <|> importAlias')))))
fromClause :: Assignment
fromClause = string
@ -1121,16 +1132,16 @@ shorthandPropertyIdentifier :: Assignment
shorthandPropertyIdentifier = makeTerm <$> symbol Grammar.ShorthandPropertyIdentifier <*> (Language.TypeScript.Syntax.ShorthandPropertyIdentifier <$> source)
requiredParameter :: Assignment
requiredParameter = makeRequiredParameter <$> symbol Grammar.RequiredParameter <*> children ((,,,) <$> (accessibilityModifier' <|> emptyTerm) <*> (identifier <|> destructuringPattern) <*> (typeAnnotation' <|> emptyTerm) <*> (expression <|> emptyTerm))
where makeRequiredParameter loc (modifier, identifier, annotation, initializer) = makeTerm loc (Language.TypeScript.Syntax.RequiredParameter [modifier, annotation] (makeTerm loc (Statement.Assignment [] identifier initializer)))
requiredParameter = makeRequiredParameter <$> symbol Grammar.RequiredParameter <*> children ((,,,,) <$> (accessibilityModifier' <|> emptyTerm) <*> (readonly' <|> emptyTerm) <*> (identifier <|> destructuringPattern) <*> (typeAnnotation' <|> emptyTerm) <*> (expression <|> emptyTerm))
where makeRequiredParameter loc (modifier, readonly, identifier, annotation, initializer) = makeTerm loc (Language.TypeScript.Syntax.RequiredParameter [modifier, readonly, annotation] (makeTerm loc (Statement.Assignment [] identifier initializer)))
restParameter :: Assignment
restParameter = makeRestParameter <$> symbol Grammar.RestParameter <*> children ((,) <$> identifier <*> (typeAnnotation' <|> emptyTerm))
where makeRestParameter loc (identifier, annotation) = makeTerm loc (Language.TypeScript.Syntax.RestParameter [annotation] identifier)
optionalParameter :: Assignment
optionalParameter = makeOptionalParam <$> symbol Grammar.OptionalParameter <*> children ((,,,) <$> (accessibilityModifier' <|> emptyTerm) <*> (identifier <|> destructuringPattern) <*> (typeAnnotation' <|> emptyTerm) <*> (expression <|> emptyTerm))
where makeOptionalParam loc (modifier, subject, annotation, initializer) = makeTerm loc (Language.TypeScript.Syntax.OptionalParameter [modifier, annotation] (makeTerm loc (Statement.Assignment [] subject initializer)))
optionalParameter = makeOptionalParam <$> symbol Grammar.OptionalParameter <*> children ((,,,,) <$> (accessibilityModifier' <|> emptyTerm) <*> (readonly' <|> emptyTerm) <*> (identifier <|> destructuringPattern) <*> (typeAnnotation' <|> emptyTerm) <*> (expression <|> emptyTerm))
where makeOptionalParam loc (modifier, readonly, subject, annotation, initializer) = makeTerm loc (Language.TypeScript.Syntax.OptionalParameter [modifier, readonly, annotation] (makeTerm loc (Statement.Assignment [] subject initializer)))
internalModule :: Assignment
internalModule = makeTerm <$> symbol Grammar.InternalModule <*> children (Language.TypeScript.Syntax.InternalModule <$> (string <|> identifier <|> nestedIdentifier) <*> statements)
@ -1181,7 +1192,7 @@ pair :: Assignment
pair = makeTerm <$> symbol Pair <*> children (Literal.KeyValue <$> propertyName <*> expression)
callExpression :: Assignment
callExpression = makeCall <$> symbol CallExpression <*> children ((,,,) <$> (expression <|> super <|> function) <*> (typeArguments <|> pure []) <*> (arguments <|> (pure <$> templateString)) <*> emptyTerm)
callExpression = makeCall <$> (symbol CallExpression <|> symbol CallExpression') <*> children ((,,,) <$> (expression <|> super <|> function) <*> (typeArguments <|> pure []) <*> (arguments <|> (pure <$> templateString)) <*> emptyTerm)
where makeCall loc (subject, typeArgs, args, body) = makeTerm loc (Expression.Call typeArgs subject args body)
arguments = symbol Arguments *> children (many (term (expression <|> spreadElement)))
typeArguments = symbol Grammar.TypeArguments *> children (some (term ty))

View File

@ -1,8 +1,10 @@
{-# LANGUAGE DataKinds, GADTs, RankNTypes, ScopedTypeVariables, TypeOperators #-}
{-# LANGUAGE ConstraintKinds, DataKinds, GADTs, RankNTypes, ScopedTypeVariables, TypeFamilies, TypeOperators #-}
module Parser
( Parser(..)
, SomeParser(..)
, someParser
-- Syntax parsers
, parserForLanguage
, syntaxParserForLanguage
-- À la carte parsers
, goParser
, jsonParser
@ -14,6 +16,7 @@ module Parser
import qualified CMarkGFM
import Data.Functor.Classes (Eq1)
import Data.Kind
import Data.Ix
import Data.Record
import qualified Data.Syntax as Syntax
@ -51,9 +54,43 @@ data Parser term where
-- | A parser for 'Markdown' using cmark.
MarkdownParser :: Parser (Term (TermF [] CMarkGFM.NodeType) (Node Markdown.Grammar))
-- | Apply all of a list of typeclasses to all of a list of functors using 'Apply'. Used by 'someParser' to constrain all of the language-specific syntax types to the typeclasses in question.
type family ApplyAll (typeclasses :: [(* -> *) -> Constraint]) (functors :: [* -> *]) :: Constraint where
ApplyAll (typeclass ': typeclasses) functors = (Apply typeclass functors, ApplyAll typeclasses functors)
ApplyAll '[] functors = ()
-- | A parser for some specific language, producing 'Term's whose syntax satisfies a list of typeclass constraints.
--
-- This enables us to abstract over the details of the specific syntax types in cases where we can describe all the requirements on the syntax with a list of typeclasses.
data SomeParser typeclasses where
SomeParser :: ApplyAll typeclasses fs => { unSomeParser :: Parser (Term (Union fs) (Record Location)) } -> SomeParser typeclasses
-- | Construct a 'SomeParser' given a proxy for a list of typeclasses and the 'Language' to be parsed, all of which must be satisfied by all of the types in the syntaxes of our supported languages.
--
-- This can be used to perform operations uniformly over terms produced by blobs with different 'Language's, and which therefore have different types in general. For example, given some 'Blob', we can parse and 'show' the parsed & assigned 'Term' like so:
--
-- > case someParser (Proxy :: Proxy '[Show1]) (blobLanguage language) of { Just (SomeParser parser) -> runTask (parse parser blob) >>= putStrLn . show ; _ -> return () }
someParser :: ( ApplyAll typeclasses JSON.Syntax
, ApplyAll typeclasses Markdown.Syntax
, ApplyAll typeclasses Python.Syntax
, ApplyAll typeclasses Ruby.Syntax
, ApplyAll typeclasses TypeScript.Syntax
)
=> proxy typeclasses -- ^ A proxy for the list of typeclasses required, e.g. @(Proxy :: Proxy '[Show1])@.
-> Language -- ^ The 'Language' to select.
-> Maybe (SomeParser typeclasses) -- ^ 'Maybe' a 'SomeParser' abstracting the syntax type to be produced.
someParser _ Go = Nothing
someParser _ JavaScript = Just (SomeParser typescriptParser)
someParser _ JSON = Just (SomeParser jsonParser)
someParser _ JSX = Just (SomeParser typescriptParser)
someParser _ Markdown = Just (SomeParser markdownParser)
someParser _ Python = Just (SomeParser pythonParser)
someParser _ Ruby = Just (SomeParser rubyParser)
someParser _ TypeScript = Just (SomeParser typescriptParser)
-- | Return a 'Language'-specific 'Parser', if one exists, falling back to the 'LineByLineParser'.
parserForLanguage :: Language -> Maybe (Parser (Term Syntax (Record DefaultFields)))
parserForLanguage language = case language of
syntaxParserForLanguage :: Language -> Maybe (Parser (Term Syntax (Record DefaultFields)))
syntaxParserForLanguage language = case language of
Go -> Just (TreeSitterParser tree_sitter_go)
JavaScript -> Just (TreeSitterParser tree_sitter_typescript)
JSON -> Just (TreeSitterParser tree_sitter_json)

View File

@ -10,8 +10,8 @@ module Renderer
, renderJSONTerm
, renderToCDiff
, renderToCTerm
, HasDeclaration
, declarationAlgebra
, markupSectionAlgebra
, syntaxDeclarationAlgebra
, Summaries(..)
, File(..)

View File

@ -1,4 +1,4 @@
{-# LANGUAGE MultiParamTypeClasses, RankNTypes, TypeOperators, ScopedTypeVariables #-}
{-# LANGUAGE DataKinds, MultiParamTypeClasses, RankNTypes, ScopedTypeVariables, TypeFamilies, TypeOperators, UndecidableInstances #-}
module Renderer.TOC
( renderToCDiff
, renderToCTerm
@ -8,8 +8,8 @@ module Renderer.TOC
, isValidSummary
, Declaration(..)
, declaration
, HasDeclaration
, declarationAlgebra
, markupSectionAlgebra
, syntaxDeclarationAlgebra
, Entry(..)
, tableOfContentsBy
@ -34,6 +34,8 @@ import Data.List (sortOn)
import Data.Maybe (fromMaybe, mapMaybe)
import Data.Output
import Data.Patch
import Data.Proxy
import Data.Range
import Data.Record
import Data.Semigroup ((<>), sconcat)
import Data.Source as Source
@ -91,6 +93,113 @@ data Declaration
| ErrorDeclaration { declarationIdentifier :: T.Text, declarationLanguage :: Maybe Language }
deriving (Eq, Generic, Show)
-- | An r-algebra producing 'Just' a 'Declaration' for syntax nodes corresponding to high-level declarations, or 'Nothing' otherwise.
--
-- Customizing this for a given syntax type involves two steps:
--
-- 1. Defining a 'CustomHasDeclaration' instance for the type.
-- 2. Adding the type to the 'DeclarationStrategy' type family.
--
-- If youre getting errors about missing a 'CustomHasDeclaration' instance for your syntax type, you probably forgot step 1.
--
-- If youre getting 'Nothing' for your syntax node at runtime, you probably forgot step 2.
declarationAlgebra :: (HasField fields Range, HasField fields Span, Foldable syntax, HasDeclaration syntax) => Blob -> RAlgebra (TermF syntax (Record fields)) (Term syntax (Record fields)) (Maybe Declaration)
declarationAlgebra blob (In ann syntax) = toDeclaration blob ann syntax
-- | Types for which we can produce a 'Declaration' in 'Maybe'. There is exactly one instance of this typeclass; adding customized 'Declaration's for a new type is done by defining an instance of 'CustomHasDeclaration' instead.
--
-- This typeclass employs the Advanced Overlap techniques designed by Oleg Kiselyov & Simon Peyton Jones: https://wiki.haskell.org/GHC/AdvancedOverlap.
class HasDeclaration syntax where
-- | Compute a 'Declaration' for a syntax type using its 'CustomHasDeclaration' instance, if any, or else falling back to the default definition (which simply returns 'Nothing').
toDeclaration :: (Foldable whole, HasField fields Range, HasField fields Span) => Blob -> Record fields -> RAlgebra syntax (Term whole (Record fields)) (Maybe Declaration)
-- | Define 'toDeclaration' using the 'CustomHasDeclaration' instance for a type if there is one or else use the default definition.
--
-- This instance determines whether or not there is an instance for @syntax@ by looking it up in the 'DeclarationStrategy' type family. Thus producing a 'Declaration' for a node requires both defining a 'CustomHasDeclaration' instance _and_ adding a definition for the type to the 'DeclarationStrategy' type family to return 'Custom'.
--
-- Note that since 'DeclarationStrategy' has a fallback case for its final entry, this instance will hold for all types of kind @* -> *@. Thus, this must be the only instance of 'HasDeclaration', as any other instance would be indistinguishable.
instance (DeclarationStrategy syntax ~ strategy, HasDeclarationWithStrategy strategy syntax) => HasDeclaration syntax where
toDeclaration = toDeclarationWithStrategy (Proxy :: Proxy strategy)
-- | Types for which we can produce a customized 'Declaration'. This returns in 'Maybe' so that some values can be opted out (e.g. anonymous functions).
class CustomHasDeclaration syntax where
-- | Produce a customized 'Declaration' for a given syntax node.
customToDeclaration :: (Foldable whole, HasField fields Range, HasField fields Span) => Blob -> Record fields -> RAlgebra syntax (Term whole (Record fields)) (Maybe Declaration)
-- | Produce a 'SectionDeclaration' from the first line of the heading of a 'Markup.Section' node.
instance CustomHasDeclaration Markup.Section where
customToDeclaration Blob{..} _ (Markup.Section level (Term (In headingAnn headingF), _) _)
= Just $ SectionDeclaration (maybe (getSource (byteRange headingAnn)) (getSource . sconcat) (nonEmpty (byteRange . termAnnotation . unTerm <$> toList headingF))) level
where getSource = firstLine . toText . flip Source.slice blobSource
firstLine = T.takeWhile (/= '\n')
-- | Produce an 'ErrorDeclaration' for 'Syntax.Error' nodes.
instance CustomHasDeclaration Syntax.Error where
customToDeclaration Blob{..} ann err@Syntax.Error{}
= Just $ ErrorDeclaration (T.pack (formatTOCError (Syntax.unError (sourceSpan ann) err))) blobLanguage
-- | Produce a 'FunctionDeclaration' for 'Declaration.Function' nodes so long as their identifier is non-empty (defined as having a non-empty 'byteRange').
instance CustomHasDeclaration Declaration.Function where
customToDeclaration Blob{..} _ (Declaration.Function _ (Term (In identifierAnn _), _) _ _)
-- Do not summarize anonymous functions
| isEmpty identifierAnn = Nothing
-- Named functions
| otherwise = Just $ FunctionDeclaration (getSource identifierAnn)
where getSource = toText . flip Source.slice blobSource . byteRange
isEmpty = (== 0) . rangeLength . byteRange
-- | Produce a 'MethodDeclaration' for 'Declaration.Method' nodes. If the methods receiver is non-empty (defined as having a non-empty 'byteRange'), the 'declarationIdentifier' will be formatted as 'receiver.method_name'; otherwise it will be simply 'method_name'.
instance CustomHasDeclaration Declaration.Method where
customToDeclaration Blob{..} _ (Declaration.Method _ (Term (In receiverAnn _), _) (Term (In identifierAnn _), _) _ _)
-- Methods without a receiver
| isEmpty receiverAnn = Just $ MethodDeclaration (getSource identifierAnn)
-- Methods with a receiver (class methods) are formatted like `receiver.method_name`
| otherwise = Just $ MethodDeclaration (getSource receiverAnn <> "." <> getSource identifierAnn)
where getSource = toText . flip Source.slice blobSource . byteRange
isEmpty = (== 0) . rangeLength . byteRange
-- | Produce a 'Declaration' for 'Union's using the 'HasDeclaration' instance & therefore using a 'CustomHasDeclaration' instance when one exists & the type is listed in 'DeclarationStrategy'.
instance Apply HasDeclaration fs => CustomHasDeclaration (Union fs) where
customToDeclaration blob ann = apply (Proxy :: Proxy HasDeclaration) (toDeclaration blob ann)
-- | A strategy for defining a 'HasDeclaration' instance. Intended to be promoted to the kind level using @-XDataKinds@.
data Strategy = Default | Custom
-- | Produce a 'Declaration' for a syntax node using either the 'Default' or 'Custom' strategy.
--
-- You should probably be using 'CustomHasDeclaration' instead of this class; and you should not define new instances of this class.
class HasDeclarationWithStrategy (strategy :: Strategy) syntax where
toDeclarationWithStrategy :: (Foldable whole, HasField fields Range, HasField fields Span) => proxy strategy -> Blob -> Record fields -> RAlgebra syntax (Term whole (Record fields)) (Maybe Declaration)
-- | A predicate on syntax types selecting either the 'Custom' or 'Default' strategy.
--
-- Only entries for which we want to use the 'Custom' strategy should be listed, with the exception of the final entry which maps all other types onto the 'Default' strategy.
--
-- If youre seeing errors about missing a 'CustomHasDeclaration' instance for a given type, youve probably listed it in here but not defined a 'CustomHasDeclaration' instance for it, or else youve listed the wrong type in here. Conversely, if your 'customHasDeclaration' method is never being called, you may have forgotten to list the type in here.
type family DeclarationStrategy syntax where
DeclarationStrategy Declaration.Function = 'Custom
DeclarationStrategy Declaration.Method = 'Custom
DeclarationStrategy Markup.Section = 'Custom
DeclarationStrategy Syntax.Error = 'Custom
DeclarationStrategy (Union fs) = 'Custom
DeclarationStrategy a = 'Default
-- | The 'Default' strategy produces 'Nothing'.
instance HasDeclarationWithStrategy 'Default syntax where
toDeclarationWithStrategy _ _ _ _ = Nothing
-- | The 'Custom' strategy delegates the selection of the strategy to the 'CustomHasDeclaration' instance for the type.
instance CustomHasDeclaration syntax => HasDeclarationWithStrategy 'Custom syntax where
toDeclarationWithStrategy _ = customToDeclaration
getDeclaration :: HasField fields (Maybe Declaration) => Record fields -> Maybe Declaration
getDeclaration = getField
@ -112,46 +221,6 @@ syntaxDeclarationAlgebra Blob{..} (In a r) = case r of
_ -> Nothing
where getSource = toText . flip Source.slice blobSource . byteRange . extract
-- | Compute 'Declaration's for methods and functions.
declarationAlgebra :: (Declaration.Function :< fs, Declaration.Method :< fs, Syntax.Empty :< fs, Syntax.Error :< fs, Apply Functor fs, HasField fields Range, HasField fields Span)
=> Blob
-> RAlgebra (TermF (Union fs) (Record fields)) (Term (Union fs) (Record fields)) (Maybe Declaration)
declarationAlgebra Blob{..} (In a r)
-- Do not summarize anonymous functions
| Just (Declaration.Function _ (identifier, _) _ _) <- prj r
, Just Syntax.Empty <- prj (unwrap identifier)
= Nothing
-- Named functions
| Just (Declaration.Function _ (identifier, _) _ _) <- prj r
= Just $ FunctionDeclaration (getSource (extract identifier))
-- Methods without a receiver
| Just (Declaration.Method _ (receiver, _) (identifier, _) _ _) <- prj r
, Just Syntax.Empty <- prj (unwrap receiver)
= Just $ MethodDeclaration (getSource (extract identifier))
-- Methods with a receiver (class methods) are formatted like `receiver.method_name`
| Just (Declaration.Method _ (receiver, _) (identifier, _) _ _) <- prj r
= Just $ MethodDeclaration (getSource (extract receiver) <> "." <> getSource (extract identifier))
| Just err@Syntax.Error{} <- prj r
= Just $ ErrorDeclaration (T.pack (formatTOCError (Syntax.unError (sourceSpan a) err))) blobLanguage
| otherwise = Nothing
where getSource = toText . flip Source.slice blobSource . byteRange
-- | Compute 'Declaration's with the headings of 'Markup.Section's.
markupSectionAlgebra :: (Markup.Section :< fs, Syntax.Error :< fs, HasField fields Range, HasField fields Span, Apply Functor fs, Apply Foldable fs)
=> Blob
-> RAlgebra (TermF (Union fs) (Record fields)) (Term (Union fs) (Record fields)) (Maybe Declaration)
markupSectionAlgebra Blob{..} (In a r)
| Just (Markup.Section level (heading, _) _) <- prj r = Just $ SectionDeclaration (maybe (getSource (extract heading)) (firstLine . toText . flip Source.slice blobSource . sconcat) (nonEmpty (byteRange . extract <$> toList (unwrap heading)))) level
| Just err@Syntax.Error{} <- prj r = Just $ ErrorDeclaration (T.pack (formatTOCError (Syntax.unError (sourceSpan a) err))) blobLanguage
| otherwise = Nothing
where getSource = firstLine . toText . flip Source.slice blobSource . byteRange
firstLine = T.takeWhile (/= '\n')
formatTOCError :: Error.Error String -> String
formatTOCError e = showExpectation False (errorExpected e) (errorActual e) ""

View File

@ -7,15 +7,18 @@ module Semantic
, diffTermPair
) where
import Algorithm (Diffable)
import Control.Exception
import Control.Monad ((<=<))
import Control.Monad.Error.Class
import Data.Bifunctor
import Data.Align.Generic
import Data.Blob
import Data.ByteString (ByteString)
import Data.Diff
import Data.Functor.Both as Both
import Data.Functor.Classes
import Data.Output
import Data.Bifoldable
import Data.Record
import Data.Syntax.Algebra
import Data.Term
@ -26,6 +29,7 @@ import qualified Language
import Parser
import Renderer
import Semantic.Task as Task
import Semantic.Stat as Stat
-- This is the primary interface to the Semantic library which provides two
-- major classes of functionality: semantic parsing and diffing of source code
@ -42,30 +46,25 @@ parseBlobs renderer = fmap toOutput . distributeFoldMap (parseBlob renderer) . f
-- | A task to parse a 'Blob' and render the resulting 'Term'.
parseBlob :: TermRenderer output -> Blob -> Task output
parseBlob renderer blob@Blob{..} = case (renderer, blobLanguage) of
(ToCTermRenderer, Just Language.Go) -> parse goParser blob >>= decorate (declarationAlgebra blob) >>= render (renderToCTerm blob)
(ToCTermRenderer, Just Language.Markdown) -> parse markdownParser blob >>= decorate (markupSectionAlgebra blob) >>= render (renderToCTerm blob)
(ToCTermRenderer, Just Language.Python) -> parse pythonParser blob >>= decorate (declarationAlgebra blob) >>= render (renderToCTerm blob)
(ToCTermRenderer, Just Language.TypeScript) -> parse typescriptParser blob >>= decorate (declarationAlgebra blob) >>= render (renderToCTerm blob)
(ToCTermRenderer, Just Language.Ruby) -> parse rubyParser blob >>= decorate (declarationAlgebra blob) >>= render (renderToCTerm blob)
(ToCTermRenderer, _) | Just syntaxParser <- syntaxParser -> parse syntaxParser blob >>= decorate (syntaxDeclarationAlgebra blob) >>= render (renderToCTerm blob)
(ToCTermRenderer, lang)
| Just (SomeParser parser) <- lang >>= someParser (Proxy :: Proxy '[HasDeclaration, Foldable, Functor]) ->
parse parser blob >>= decorate (declarationAlgebra blob) >>= render (renderToCTerm blob)
| Just syntaxParser <- lang >>= syntaxParserForLanguage ->
parse syntaxParser blob >>= decorate (syntaxDeclarationAlgebra blob) >>= render (renderToCTerm blob)
(JSONTermRenderer, Just Language.Go) -> parse goParser blob >>= decorate constructorLabel >>= render (renderJSONTerm blob)
(JSONTermRenderer, Just Language.JSON) -> parse jsonParser blob >>= decorate constructorLabel >>= render (renderJSONTerm blob)
(JSONTermRenderer, Just Language.Markdown) -> parse markdownParser blob >>= decorate constructorLabel >>= render (renderJSONTerm blob)
(JSONTermRenderer, Just Language.Python) -> parse pythonParser blob >>= decorate constructorLabel >>= render (renderJSONTerm blob)
(JSONTermRenderer, Just Language.TypeScript) -> parse typescriptParser blob >>= decorate constructorLabel >>= render (renderJSONTerm blob)
(JSONTermRenderer, Just Language.Ruby) -> parse rubyParser blob >>= decorate constructorLabel >>= render (renderJSONTerm blob)
(JSONTermRenderer, _) | Just syntaxParser <- syntaxParser -> parse syntaxParser blob >>= decorate syntaxIdentifierAlgebra >>= render (renderJSONTerm blob)
(JSONTermRenderer, lang)
| Just (SomeParser parser) <- lang >>= someParser (Proxy :: Proxy '[ConstructorName, Foldable, Functor]) ->
parse parser blob >>= decorate constructorLabel >>= render (renderJSONTerm blob)
| Just syntaxParser <- lang >>= syntaxParserForLanguage ->
parse syntaxParser blob >>= decorate syntaxIdentifierAlgebra >>= render (renderJSONTerm blob)
(SExpressionTermRenderer, lang)
| Just (SomeParser parser) <- lang >>= someParser (Proxy :: Proxy '[ConstructorName, Foldable, Functor]) ->
parse parser blob >>= decorate constructorLabel . (Nil <$) >>= render renderSExpressionTerm
| Just syntaxParser <- lang >>= syntaxParserForLanguage ->
parse syntaxParser blob >>= render renderSExpressionTerm . fmap keepCategory
(SExpressionTermRenderer, Just Language.Go) -> parse goParser blob >>= decorate constructorLabel >>= render renderSExpressionTerm . fmap keepConstructorLabel
(SExpressionTermRenderer, Just Language.JSON) -> parse jsonParser blob >>= decorate constructorLabel >>= render renderSExpressionTerm . fmap keepConstructorLabel
(SExpressionTermRenderer, Just Language.Markdown) -> parse markdownParser blob >>= decorate constructorLabel >>= render renderSExpressionTerm . fmap keepConstructorLabel
(SExpressionTermRenderer, Just Language.Python) -> parse pythonParser blob >>= decorate constructorLabel >>= render renderSExpressionTerm . fmap keepConstructorLabel
(SExpressionTermRenderer, Just Language.TypeScript) -> parse typescriptParser blob >>= decorate constructorLabel >>= render renderSExpressionTerm . fmap keepConstructorLabel
(SExpressionTermRenderer, Just Language.Ruby) -> parse rubyParser blob >>= decorate constructorLabel >>= render renderSExpressionTerm . fmap keepConstructorLabel
(SExpressionTermRenderer, _) | Just syntaxParser <- syntaxParser -> parse syntaxParser blob >>= render renderSExpressionTerm . fmap keepCategory
_ -> throwError (SomeException (NoParserForLanguage blobPath blobLanguage))
where syntaxParser = blobLanguage >>= parserForLanguage
data NoParserForLanguage = NoParserForLanguage FilePath (Maybe Language.Language)
deriving (Eq, Exception, Ord, Show, Typeable)
@ -77,67 +76,61 @@ diffBlobPairs renderer = fmap toOutput . distributeFoldMap (diffBlobPair rendere
-- | A task to parse a pair of 'Blob's, diff them, and render the 'Diff'.
diffBlobPair :: DiffRenderer output -> Both Blob -> Task output
diffBlobPair renderer blobs = case (renderer, effectiveLanguage) of
(OldToCDiffRenderer, Just Language.Markdown) -> run (\ blob -> parse markdownParser blob >>= decorate (markupSectionAlgebra blob)) diffTerms (renderToCDiff blobs)
(OldToCDiffRenderer, Just Language.Python) -> run (\ blob -> parse pythonParser blob >>= decorate (declarationAlgebra blob)) diffTerms (renderToCDiff blobs)
(OldToCDiffRenderer, Just Language.Ruby) -> run (\ blob -> parse rubyParser blob >>= decorate (declarationAlgebra blob)) diffTerms (renderToCDiff blobs)
(OldToCDiffRenderer, _) | Just syntaxParser <- syntaxParser -> run (\ blob -> parse syntaxParser blob >>= decorate (syntaxDeclarationAlgebra blob)) diffSyntaxTerms (renderToCDiff blobs)
(OldToCDiffRenderer, lang)
| lang `elem` [ Just Language.Markdown, Just Language.Python, Just Language.Ruby ]
, Just (SomeParser parser) <- lang >>= someParser (Proxy :: Proxy '[Diffable, Eq1, Foldable, Functor, GAlign, HasDeclaration, Show1, Traversable]) ->
run (\ blob -> parse parser blob >>= decorate (declarationAlgebra blob)) diffTerms (renderToCDiff blobs)
| Just syntaxParser <- lang >>= syntaxParserForLanguage ->
run (\ blob -> parse syntaxParser blob >>= decorate (syntaxDeclarationAlgebra blob)) diffSyntaxTerms (renderToCDiff blobs)
(ToCDiffRenderer, Just Language.Go) -> run (\ blob -> parse goParser blob >>= decorate (declarationAlgebra blob)) diffTerms (renderToCDiff blobs)
(ToCDiffRenderer, Just Language.Markdown) -> run (\ blob -> parse markdownParser blob >>= decorate (markupSectionAlgebra blob)) diffTerms (renderToCDiff blobs)
(ToCDiffRenderer, Just Language.Python) -> run (\ blob -> parse pythonParser blob >>= decorate (declarationAlgebra blob)) diffTerms (renderToCDiff blobs)
(ToCDiffRenderer, Just Language.Ruby) -> run (\ blob -> parse rubyParser blob >>= decorate (declarationAlgebra blob)) diffTerms (renderToCDiff blobs)
(ToCDiffRenderer, Just Language.TypeScript) -> run (\ blob -> parse typescriptParser blob >>= decorate (declarationAlgebra blob)) diffTerms (renderToCDiff blobs)
(ToCDiffRenderer, _) | Just syntaxParser <- syntaxParser -> run (\ blob -> parse syntaxParser blob >>= decorate (syntaxDeclarationAlgebra blob)) diffSyntaxTerms (renderToCDiff blobs)
(ToCDiffRenderer, lang)
| Just (SomeParser parser) <- lang >>= someParser (Proxy :: Proxy '[Diffable, Eq1, Foldable, Functor, GAlign, HasDeclaration, Show1, Traversable]) ->
run (\ blob -> parse parser blob >>= decorate (declarationAlgebra blob)) diffTerms (renderToCDiff blobs)
| Just syntaxParser <- lang >>= syntaxParserForLanguage ->
run (\ blob -> parse syntaxParser blob >>= decorate (syntaxDeclarationAlgebra blob)) diffSyntaxTerms (renderToCDiff blobs)
(JSONDiffRenderer, Just Language.Go) -> run (parse goParser) diffTerms (renderJSONDiff blobs)
(JSONDiffRenderer, Just Language.JSON) -> run (parse jsonParser) diffTerms (renderJSONDiff blobs)
(JSONDiffRenderer, Just Language.Markdown) -> run (parse markdownParser) diffTerms (renderJSONDiff blobs)
(JSONDiffRenderer, Just Language.Python) -> run (parse pythonParser) diffTerms (renderJSONDiff blobs)
(JSONDiffRenderer, Just Language.Ruby) -> run (parse rubyParser) diffTerms (renderJSONDiff blobs)
(JSONDiffRenderer, Just Language.TypeScript) -> run (parse typescriptParser) diffTerms (renderJSONDiff blobs)
(JSONDiffRenderer, _) | Just syntaxParser <- syntaxParser -> run (decorate syntaxIdentifierAlgebra <=< parse syntaxParser) diffSyntaxTerms (renderJSONDiff blobs)
(JSONDiffRenderer, lang)
| Just (SomeParser parser) <- lang >>= someParser (Proxy :: Proxy '[Diffable, Eq1, Foldable, Functor, GAlign, Show1, Traversable]) ->
run (parse parser) diffTerms (renderJSONDiff blobs)
| Just syntaxParser <- lang >>= syntaxParserForLanguage ->
run (decorate syntaxIdentifierAlgebra <=< parse syntaxParser) diffSyntaxTerms (renderJSONDiff blobs)
(PatchDiffRenderer, Just Language.Go) -> run (parse goParser) diffTerms (renderPatch blobs)
(PatchDiffRenderer, Just Language.JSON) -> run (parse jsonParser) diffTerms (renderPatch blobs)
(PatchDiffRenderer, Just Language.Markdown) -> run (parse markdownParser) diffTerms (renderPatch blobs)
(PatchDiffRenderer, Just Language.Python) -> run (parse pythonParser) diffTerms (renderPatch blobs)
(PatchDiffRenderer, Just Language.Ruby) -> run (parse rubyParser) diffTerms (renderPatch blobs)
(PatchDiffRenderer, Just Language.TypeScript) -> run (parse typescriptParser) diffTerms (renderPatch blobs)
(PatchDiffRenderer, _) | Just syntaxParser <- syntaxParser -> run (parse syntaxParser) diffSyntaxTerms (renderPatch blobs)
(PatchDiffRenderer, lang)
| Just (SomeParser parser) <- lang >>= someParser (Proxy :: Proxy '[Diffable, Eq1, Foldable, Functor, GAlign, Show1, Traversable]) ->
run (parse parser) diffTerms (renderPatch blobs)
| Just syntaxParser <- lang >>= syntaxParserForLanguage ->
run (parse syntaxParser) diffSyntaxTerms (renderPatch blobs)
(SExpressionDiffRenderer, lang)
| Just (SomeParser parser) <- lang >>= someParser (Proxy :: Proxy '[ConstructorName, Diffable, Eq1, Foldable, Functor, GAlign, Show1, Traversable]) ->
run (decorate constructorLabel . (Nil <$) <=< parse parser) diffTerms renderSExpressionDiff
| Just syntaxParser <- lang >>= syntaxParserForLanguage ->
run (fmap (fmap keepCategory) . parse syntaxParser) diffSyntaxTerms renderSExpressionDiff
(SExpressionDiffRenderer, Just Language.Go) -> run (decorate constructorLabel <=< parse goParser) diffTerms (renderSExpressionDiff . bimap keepConstructorLabel keepConstructorLabel)
(SExpressionDiffRenderer, Just Language.JSON) -> run (decorate constructorLabel <=< parse jsonParser) diffTerms (renderSExpressionDiff . bimap keepConstructorLabel keepConstructorLabel)
(SExpressionDiffRenderer, Just Language.Markdown) -> run (decorate constructorLabel <=< parse markdownParser) diffTerms (renderSExpressionDiff . bimap keepConstructorLabel keepConstructorLabel)
(SExpressionDiffRenderer, Just Language.Python) -> run (decorate constructorLabel <=< parse pythonParser) diffTerms (renderSExpressionDiff . bimap keepConstructorLabel keepConstructorLabel)
(SExpressionDiffRenderer, Just Language.Ruby) -> run (decorate constructorLabel <=< parse rubyParser) diffTerms (renderSExpressionDiff . bimap keepConstructorLabel keepConstructorLabel)
(SExpressionDiffRenderer, Just Language.TypeScript) -> run (decorate constructorLabel <=< parse typescriptParser) diffTerms (renderSExpressionDiff . bimap keepConstructorLabel keepConstructorLabel)
(SExpressionDiffRenderer, _) | Just syntaxParser <- syntaxParser -> run (parse syntaxParser) diffSyntaxTerms (renderSExpressionDiff . bimap keepCategory keepCategory)
_ -> throwError (SomeException (NoParserForLanguage effectivePath effectiveLanguage))
where (effectivePath, effectiveLanguage) = case runJoin blobs of
(Blob { blobLanguage = Just lang, blobPath = path }, _) -> (path, Just lang)
(_, Blob { blobLanguage = Just lang, blobPath = path }) -> (path, Just lang)
(Blob { blobPath = path }, _) -> (path, Nothing)
syntaxParser = effectiveLanguage >>= parserForLanguage
run :: Functor syntax => (Blob -> Task (Term syntax ann)) -> (Term syntax ann -> Term syntax ann -> Diff syntax ann ann) -> (Diff syntax ann ann -> output) -> Task output
run parse diff renderer = distributeFor blobs parse >>= runBothWith (diffTermPair blobs diff) >>= render renderer
run :: (Foldable syntax, Functor syntax) => (Blob -> Task (Term syntax ann)) -> (Term syntax ann -> Term syntax ann -> Diff syntax ann ann) -> (Diff syntax ann ann -> output) -> Task output
run parse diff renderer = do
terms <- distributeFor blobs parse
time "diff" languageTag $ do
diff <- runBothWith (diffTermPair blobs diff) terms
writeStat (Stat.count "diff.nodes" (bilength diff) languageTag)
render renderer diff
where
showLanguage = pure . (,) "language" . show
languageTag = let (a, b) = runJoin blobs
in maybe (maybe [] showLanguage (blobLanguage b)) showLanguage (blobLanguage a)
-- | A task to diff a pair of 'Term's, producing insertion/deletion 'Patch'es for non-existent 'Blob's.
diffTermPair :: Functor syntax => Both Blob -> Differ syntax ann1 ann2 -> Term syntax ann1 -> Term syntax ann2 -> Task (Diff syntax ann1 ann2)
diffTermPair blobs differ t1 t2 = case runJoin (blobExists <$> blobs) of
(True, False) -> pure (deleting t1)
(False, True) -> pure (inserting t2)
_ -> time "diff" logInfo $ diff differ t1 t2
where
logInfo = let (a, b) = runJoin blobs in
[ ("before_path", blobPath a)
, ("before_language", maybe "" show (blobLanguage a))
, ("after_path", blobPath b)
, ("after_language", maybe "" show (blobLanguage b)) ]
_ -> diff differ t1 t2
keepCategory :: HasField fields Category => Record fields -> Record '[Category]
keepCategory = (:. Nil) . category
keepConstructorLabel :: Record (ConstructorLabel ': fields) -> Record '[ConstructorLabel]
keepConstructorLabel = (:. Nil) . rhead

View File

@ -5,14 +5,17 @@ import Data.Error (withSGRCode)
import Data.Foldable (toList)
import Data.List (intersperse)
import Data.Semigroup ((<>))
import qualified Data.Time.Clock.POSIX as Time (getCurrentTime)
import qualified Data.Time.Format as Time
import qualified Data.Time.LocalTime as LocalTime
import Semantic.Queue
import System.Console.ANSI
import System.IO (Handle, hIsTerminalDevice)
import System.IO
import System.Posix.Process
import System.Posix.Types
import Text.Printf
-- | A log message at a specific level.
data Message = Message Level String [(String, String)] LocalTime.ZonedTime
deriving (Show)
@ -24,6 +27,17 @@ data Level
| Debug
deriving (Eq, Ord, Show)
-- | Queue a message to be logged.
queueLogMessage :: AsyncQueue Message Options -> Level -> String -> [(String, String)] -> IO ()
queueLogMessage q@AsyncQueue{..} level message pairs
| Just logLevel <- optionsLevel asyncQueueExtra, level <= logLevel = Time.getCurrentTime >>= LocalTime.utcToLocalZonedTime >>= queue q . Message level message pairs
| otherwise = pure ()
-- | Log a message to stderr.
logMessage :: Options -> Message -> IO ()
logMessage options@Options{..} = hPutStr stderr . optionsFormatter options
-- | Format log messaging using "logfmt".
--
-- Logfmt is a loosely defined logging format (see https://brandur.org/logfmt)

54
src/Semantic/Queue.hs Normal file
View File

@ -0,0 +1,54 @@
module Semantic.Queue
(
AsyncQueue(..)
, newQueue
, newQueue'
, queue
, closeQueue
)
where
import Control.Concurrent.Async as Async
import Control.Concurrent.STM.TMQueue
import GHC.Conc
-- | 'AsyncQueue' represents a 'TMQueue' that's drained from a separate thread.
-- It is intended to be used to queue data from a pure function and then process
-- that data in IO on a separate thread. 'AsyncQueue' is parameterized by:
-- * 'a' - the type of message stored on the queue.
-- * 'extra' - any other type needed to process messages on the queue.
data AsyncQueue a extra
= AsyncQueue
{ asyncQueue :: TMQueue a -- ^ The underlying 'TMQueue'.
, asyncQueueSink :: Async () -- ^ A sink that will drain the queue.
, asyncQueueExtra :: extra -- ^ Any exta data the queue needs to use.
}
-- | Create a new AsyncQueue using the default sink.
newQueue :: (extra -> a -> IO ()) -> extra -> IO (AsyncQueue a extra)
newQueue = newQueue' . sink
-- | Create a new AsyncQueue, specifying a custom sink.
newQueue' :: (extra -> TMQueue a -> IO ()) -> extra -> IO (AsyncQueue a extra)
newQueue' f extra = do
q <- newTMQueueIO
s <- Async.async (f extra q)
pure (AsyncQueue q s extra)
-- | Queue a message.
queue :: AsyncQueue a extra -> a -> IO ()
queue AsyncQueue{..} = atomically . writeTMQueue asyncQueue
-- | Drain messages from the queue, calling the specified function for each message.
sink :: (extra -> a -> IO ()) -> extra -> TMQueue a -> IO ()
sink f extra q = do
msg <- atomically (readTMQueue q)
maybe (pure ()) go msg
where go msg = f extra msg >> sink f extra q
-- | Close the queue.
closeQueue :: AsyncQueue a extra -> IO ()
closeQueue AsyncQueue{..} = do
atomically (closeTMQueue asyncQueue)
Async.wait asyncQueueSink

199
src/Semantic/Stat.hs Normal file
View File

@ -0,0 +1,199 @@
module Semantic.Stat
(
-- Primary API for creating stats.
increment
, decrement
, count
, gauge
, timing
, withTiming
, histogram
, set
, Stat
-- Client
, defaultStatsClient
, StatsClient(..)
-- Internal, exposed for testing
, renderDatagram
, sendStat
) where
import Data.Functor
import Data.List (intercalate)
import Data.List.Split (splitOneOf)
import Data.Maybe
import Data.Monoid
import Network.Socket (Socket(..), SocketType(..), socket, connect, getAddrInfo, addrFamily, addrAddress, defaultProtocol)
import Network.Socket.ByteString
import Network.URI
import Numeric
import qualified Data.ByteString.Char8 as B
import System.Environment
import System.IO.Error
import qualified Data.Time.Clock as Time
import qualified Data.Time.Clock.POSIX as Time (getCurrentTime)
-- | A named piece of data you wish to record a specific 'Metric' for.
-- See https://docs.datadoghq.com/guides/dogstatsd/ for more details.
data Stat
= Stat
{ statName :: String -- ^ Stat name, usually separated by '.' (e.g. "system.metric.name")
, statValue :: Metric -- ^ 'Metric' value.
, statTags :: Tags -- ^ Key/value 'Tags' (optional).
}
-- | The various supported metric types in Datadog.
data Metric
= Counter Int -- ^ Counters track how many times something happens per second.
| Gauge Double -- ^ Gauges track the ebb and flow of a particular metric value over time.
| Histogram Double -- ^ Histograms calculate the statistical distribution of any kind of value.
| Set Double -- ^ Sets count the number of unique elements in a group
| Timer Double -- ^ Timers measure the amount of time a section of code takes to execute.
-- | Tags are key/value annotations. Values can blank.
type Tags = [(String, String)]
-- | Increment a counter.
increment :: String -> Tags -> Stat
increment n = count n 1
-- | Decrement a counter.
decrement :: String -> Tags -> Stat
decrement n = count n (-1)
-- | Arbitrary count.
count :: String -> Int -> Tags -> Stat
count n v = Stat n (Counter v)
-- | Arbitrary gauge value.
gauge :: String -> Double -> Tags -> Stat
gauge n v = Stat n (Gauge v)
-- | Timing in milliseconds.
timing :: String -> Double -> Tags -> Stat
timing n v = Stat n (Timer v)
-- | Run an IO Action and record timing
withTiming :: (Stat -> IO ()) -> String -> Tags -> IO a -> IO a
withTiming statter name tags f = do
start <- Time.getCurrentTime
result <- f
end <- Time.getCurrentTime
let duration = realToFrac (Time.diffUTCTime end start * 1000)
statter (timing name duration tags)
pure result
-- | Histogram measurement.
histogram :: String -> Double -> Tags -> Stat
histogram n v = Stat n (Histogram v)
-- | Set counter.
set :: String -> Double -> Tags -> Stat
set n v = Stat n (Set v)
-- | Client for sending stats on a UDP socket.
data StatsClient
= StatsClient
{ statsClientUDPSocket :: Socket
, statsClientNamespace :: String
, statsClientUDPHost :: String
, statsClientUDPPort :: String
}
-- | Create a default stats client. This function consults two optional
-- environment variables for the stats URI (default: 127.0.0.1:28125).
-- * STATS_ADDR - String URI to send stats to in the form of `host:port`.
-- * DOGSTATSD_HOST - String hostname which will override the above host.
-- Generally used on kubes pods.
defaultStatsClient :: IO StatsClient
defaultStatsClient = do
addr <- lookupEnv "STATS_ADDR"
let (host', port) = parseAddr (fmap ("statsd://" <>) addr)
-- When running in Kubes, DOGSTATSD_HOST is set with the dogstatsd host.
kubesHost <- lookupEnv "DOGSTATSD_HOST"
let host = fromMaybe host' kubesHost
statsClient host port "semantic"
where
defaultHost = "127.0.0.1"
defaultPort = "28125"
parseAddr a | Just s <- a
, Just (Just (URIAuth _ host port)) <- uriAuthority <$> parseURI s
= (parseHost host, parsePort port)
| otherwise = (defaultHost, defaultPort)
parseHost s = if null s then defaultHost else s
parsePort s = if null s then defaultPort else dropWhile (':' ==) s
-- | Create a StatsClient at the specified host and port with a namespace prefix.
statsClient :: String -> String -> String -> IO StatsClient
statsClient host port statsClientNamespace = do
(addr:_) <- getAddrInfo Nothing (Just host) (Just port)
sock <- socket (addrFamily addr) Datagram defaultProtocol
connect sock (addrAddress addr)
pure (StatsClient sock statsClientNamespace host port)
-- | Send a stat over the StatsClient's socket.
sendStat :: StatsClient -> Stat -> IO ()
sendStat StatsClient{..} = void . tryIOError . sendAll statsClientUDPSocket . B.pack . renderDatagram statsClientNamespace
-- Datagram Rendering
-- | Rendering of stats to their datagrams representations, which are packed and
-- sent over a socket.
class Render a where
renders :: a -> RenderS
-- | A Function that prepends the output 'String' to an existing 'String'.
-- Analogous to 'ShowS'.
type RenderS = String -> String
-- | Utility function to prepend the string unchanged.
renderString :: String -> RenderS
renderString = (<>)
-- | Internal: Clean a stat name of reserved chars `|, @, :`
clean :: String -> String
clean = intercalate "_" . splitOneOf "|@:"
-- | Render a Stat (with namespace prefix) to a datagram String.
renderDatagram :: String -> Stat -> String
renderDatagram namespace stat = renderString prefix (renders stat "")
where prefix | null namespace = ""
| otherwise = clean namespace <> "."
-- Instances
instance Render Stat where
renders Stat{..}
= renderString (clean statName)
. renderString ":"
. renders statValue
. renders statTags
instance Render Metric where
renders (Counter x) = renders x . renderString "|c"
renders (Gauge x) = renders x . renderString "|g"
renders (Histogram x) = renders x . renderString "|h"
renders (Set x) = renders x . renderString "|s"
renders (Timer x) = renders x . renderString "|ms"
instance Render Tags where
renders [] = renderString ""
renders xs = renderString "|#" . (\x -> x <> intercalate "," (renderTag <$> xs))
where
renderTag (k, "") = k
renderTag (k, v) = k <> ":" <> v
instance Render Int where
renders = shows
instance Render Double where
renders = showFFloat (Just 5)

View File

@ -1,4 +1,4 @@
{-# LANGUAGE DataKinds, GADTs, MultiParamTypeClasses, TypeOperators, BangPatterns #-}
{-# LANGUAGE DataKinds, GADTs, MultiParamTypeClasses, TypeOperators #-}
module Semantic.Task
( Task
, Level(..)
@ -8,6 +8,7 @@ module Semantic.Task
, readBlobPairs
, writeToOutput
, writeLog
, writeStat
, time
, parse
, decorate
@ -24,7 +25,6 @@ module Semantic.Task
, runTaskWithOptions
) where
import Control.Concurrent.STM.TMQueue
import Control.Exception
import Control.Monad.Error.Class
import Control.Monad.IO.Class
@ -39,31 +39,30 @@ import Data.Foldable (fold, for_)
import Data.Functor.Both as Both hiding (snd)
import Data.Functor.Foldable (cata)
import Data.Record
import Data.Semigroup ((<>))
import qualified Data.Syntax as Syntax
import Data.Syntax.Algebra (RAlgebra, decoratorWithAlgebra)
import qualified Data.Syntax.Assignment as Assignment
import Data.Term
import qualified Data.Time.Clock as Time
import qualified Data.Time.Clock.POSIX as Time (getCurrentTime)
import qualified Data.Time.LocalTime as LocalTime
import Data.Union
import Info hiding (Category(..))
import qualified Files
import GHC.Conc (atomically)
import Language
import Language.Markdown
import Parser
import System.Exit (die)
import System.IO (Handle, hPutStr, stderr)
import System.IO (Handle, stderr)
import TreeSitter
import Semantic.Log
import Semantic.Stat as Stat
import Semantic.Queue
data TaskF output where
ReadBlobs :: Either Handle [(FilePath, Maybe Language)] -> TaskF [Blob]
ReadBlobPairs :: Either Handle [Both (FilePath, Maybe Language)] -> TaskF [Both Blob]
WriteToOutput :: Either Handle FilePath -> B.ByteString -> TaskF ()
WriteLog :: Level -> String -> [(String, String)] -> TaskF ()
WriteStat :: Stat -> TaskF ()
Time :: String -> [(String, String)] -> Task output -> TaskF output
Parse :: Parser term -> Blob -> TaskF term
Decorate :: Functor f => RAlgebra (TermF f (Record fields)) (Term f (Record fields)) field -> Term f (Record fields) -> TaskF (Term f (Record (field ': fields)))
@ -99,14 +98,17 @@ readBlobPairs from = ReadBlobPairs from `Then` return
writeToOutput :: Either Handle FilePath -> B.ByteString -> Task ()
writeToOutput path contents = WriteToOutput path contents `Then` return
-- | A 'Task' which logs a message at a specific log level to stderr.
writeLog :: Level -> String -> [(String, String)] -> Task ()
writeLog level message pairs = WriteLog level message pairs `Then` return
-- | A 'Task' which measures and logs the timing of another 'Task'.
-- | A 'Task' which writes a stat.
writeStat :: Stat -> Task ()
writeStat stat = WriteStat stat `Then` return
-- | A 'Task' which measures and stats the timing of another 'Task'.
time :: String -> [(String, String)] -> Task output -> Task output
time message pairs task = Time message pairs task `Then` return
time statName tags task = Time statName tags task `Then` return
-- | A 'Task' which parses a 'Blob' with the given 'Parser'.
parse :: Parser term -> Blob -> Task term
@ -148,82 +150,86 @@ distributeFoldMap toTask inputs = fmap fold (distribute (fmap toTask inputs))
runTask :: Task a -> IO a
runTask = runTaskWithOptions defaultOptions
-- | Execute a 'Task' with the passed 'Options', yielding its result value in 'IO'.
runTaskWithOptions :: Options -> Task a -> IO a
runTaskWithOptions options task = do
options <- configureOptionsForHandle stderr options
logQueue <- newTMQueueIO
logging <- Async.async (logSink options logQueue)
statter <- defaultStatsClient >>= newQueue sendStat
logger <- newQueue logMessage options
result <- run options logQueue task
atomically (closeTMQueue logQueue)
Async.wait logging
result <- withTiming (queue statter) "run" [] $
run options logger statter task
closeQueue statter
closeQueue logger
either (die . displayException) pure result
where logSink options@Options{..} queue = do
message <- atomically (readTMQueue queue)
case message of
Just message -> do
hPutStr stderr (optionsFormatter options message)
logSink options queue
_ -> pure ()
run :: Options -> TMQueue Message -> Task a -> IO (Either SomeException a)
run options logQueue = go
where go :: Task a -> IO (Either SomeException a)
go = iterFreerA (\ task yield -> case task of
ReadBlobs source -> (either Files.readBlobsFromHandle (traverse (uncurry Files.readFile)) source >>= yield) `catchError` (pure . Left . toException)
ReadBlobPairs source -> (either Files.readBlobPairsFromHandle (traverse (traverse (uncurry Files.readFile))) source >>= yield) `catchError` (pure . Left . toException)
WriteToOutput destination contents -> either B.hPutStr B.writeFile destination contents >>= yield
WriteLog level message pairs -> queueLogMessage level message pairs >>= yield
Time message pairs task -> do
start <- Time.getCurrentTime
!res <- go task
end <- Time.getCurrentTime
queueLogMessage Info message (pairs <> [("duration", show (Time.diffUTCTime end start))])
either (pure . Left) yield res
Parse parser blob -> go (runParser options blob parser) >>= either (pure . Left) yield
Decorate algebra term -> pure (decoratorWithAlgebra algebra term) >>= yield
Semantic.Task.Diff differ term1 term2 -> pure (differ term1 term2) >>= yield
Render renderer input -> pure (renderer input) >>= yield
Distribute tasks -> Async.mapConcurrently go tasks >>= either (pure . Left) yield . sequenceA . withStrategy (parTraversable (parTraversable rseq))
LiftIO action -> action >>= yield
Throw err -> pure (Left err)
Catch during handler -> do
result <- go during
case result of
Left err -> go (handler err) >>= either (pure . Left) yield
Right a -> yield a) . fmap Right
queueLogMessage level message pairs
| Just logLevel <- optionsLevel options, level <= logLevel = Time.getCurrentTime >>= LocalTime.utcToLocalZonedTime >>= atomically . writeTMQueue logQueue . Message level message pairs
| otherwise = pure ()
where
run :: Options
-> AsyncQueue Message Options
-> AsyncQueue Stat StatsClient
-> Task a
-> IO (Either SomeException a)
run options logger statter = go
where
go :: Task a -> IO (Either SomeException a)
go = iterFreerA (\ task yield -> case task of
ReadBlobs source -> (either Files.readBlobsFromHandle (traverse (uncurry Files.readFile)) source >>= yield) `catchError` (pure . Left . toException)
ReadBlobPairs source -> (either Files.readBlobPairsFromHandle (traverse (traverse (uncurry Files.readFile))) source >>= yield) `catchError` (pure . Left . toException)
WriteToOutput destination contents -> either B.hPutStr B.writeFile destination contents >>= yield
WriteLog level message pairs -> queueLogMessage logger level message pairs >>= yield
WriteStat stat -> queue statter stat >>= yield
Time statName tags task -> withTiming (queue statter) statName tags (go task) >>= either (pure . Left) yield
Parse parser blob -> go (runParser options blob parser) >>= either (pure . Left) yield
Decorate algebra term -> pure (decoratorWithAlgebra algebra term) >>= yield
Semantic.Task.Diff differ term1 term2 -> pure (differ term1 term2) >>= yield
Render renderer input -> pure (renderer input) >>= yield
Distribute tasks -> Async.mapConcurrently go tasks >>= either (pure . Left) yield . sequenceA . withStrategy (parTraversable (parTraversable rseq))
LiftIO action -> action >>= yield
Throw err -> pure (Left err)
Catch during handler -> do
result <- go during
case result of
Left err -> go (handler err) >>= either (pure . Left) yield
Right a -> yield a) . fmap Right
runParser :: Options -> Blob -> Parser term -> Task term
runParser Options{..} blob@Blob{..} = go
where go :: Parser term -> Task term
go parser = case parser of
ASTParser language ->
logTiming "ts ast parse" $
liftIO ((Right <$> parseToAST language blob) `catchError` (pure . Left . toException)) >>= either throwError pure
AssignmentParser parser assignment -> do
ast <- go parser `catchError` \ err -> writeLog Error "failed parsing" (("tag", "parse") : blobFields) >> throwError err
logTiming "assign" $ case Assignment.assign blobSource assignment ast of
Left err -> do
let formatted = Error.formatError optionsPrintSource (optionsIsTerminal && optionsEnableColour) blob err
writeLog Error formatted (("tag", "assign") : blobFields)
throwError (toException err)
Right term -> do
for_ (errors term) $ \ err ->
writeLog Warning (Error.formatError optionsPrintSource (optionsIsTerminal && optionsEnableColour) blob err) (("tag", "assign") : blobFields)
pure term
TreeSitterParser tslanguage -> logTiming "ts parse" $ liftIO (treeSitterParser tslanguage blob)
MarkdownParser -> logTiming "cmark parse" $ pure (cmarkParser blobSource)
blobFields = ("path", blobPath) : maybe [] (pure . (,) "language" . show) blobLanguage
errors :: (Syntax.Error :< fs, Apply Foldable fs, Apply Functor fs) => Term (Union fs) (Record Assignment.Location) -> [Error.Error String]
errors = cata $ \ (In a syntax) -> case syntax of
_ | Just err@Syntax.Error{} <- prj syntax -> [Syntax.unError (sourceSpan a) err]
_ -> fold syntax
logTiming :: String -> Task a -> Task a
logTiming msg = time msg blobFields
where
go :: Parser term -> Task term
go parser = case parser of
ASTParser language ->
time "parse.tree_sitter_ast_parse" languageTag $
liftIO ((Right <$> parseToAST language blob) `catchError` (pure . Left . toException)) >>= either throwError pure
AssignmentParser parser assignment -> do
ast <- go parser `catchError` \ err -> do
writeStat (Stat.increment "parse.parse_failures" languageTag)
writeLog Error "failed parsing" (("tag", "parse") : blobFields)
throwError err
time "parse.assign" languageTag $
case Assignment.assign blobSource assignment ast of
Left err -> do
writeStat (Stat.increment "parse.assign_errors" languageTag)
writeLog Error (Error.formatError optionsPrintSource (optionsIsTerminal && optionsEnableColour) blob err) (("tag", "assign") : blobFields)
throwError (toException err)
Right term -> do
for_ (errors term) $ \ err -> do
writeStat (Stat.increment "parse.parse_errors" languageTag)
writeLog Warning (Error.formatError optionsPrintSource (optionsIsTerminal && optionsEnableColour) blob err) (("tag", "assign") : blobFields)
writeStat (Stat.count "parse.nodes" (length term) languageTag)
pure term
TreeSitterParser tslanguage ->
time "parse.tree_sitter_parse" languageTag $
liftIO (treeSitterParser tslanguage blob)
MarkdownParser ->
time "parse.cmark_parse" languageTag $
pure (cmarkParser blobSource)
blobFields = ("path", blobPath) : languageTag
languageTag = maybe [] (pure . (,) ("language" :: String) . show) blobLanguage
errors :: (Syntax.Error :< fs, Apply Foldable fs, Apply Functor fs) => Term (Union fs) (Record Assignment.Location) -> [Error.Error String]
errors = cata $ \ (In a syntax) -> case syntax of
_ | Just err@Syntax.Error{} <- prj syntax -> [Syntax.unError (sourceSpan a) err]
_ -> fold syntax
instance MonadIO Task where
liftIO action = LiftIO action `Then` return

View File

@ -1,6 +1,8 @@
{-# LANGUAGE TypeOperators, DataKinds #-}
-- MonoLocalBinds is to silence a warning about a simplifiable constraint.
{-# LANGUAGE DataKinds, MonoLocalBinds, TypeOperators #-}
module Semantic.Util where
import Control.Monad.IO.Class
import Data.Blob
import Language.Haskell.HsColour (hscolour, Output(TTY))
import Language.Haskell.HsColour.Colourise (defaultColourPrefs)
@ -18,34 +20,26 @@ import Data.Diff
import Semantic
import Semantic.Task
import Renderer.TOC
import Data.Union
import Data.Syntax.Declaration as Declaration
import Data.Range
import Data.Span
import Data.Syntax
-- Produces colorized pretty-printed output for the terminal / GHCi.
pp :: Show a => a -> IO ()
pp = putStrLn . hscolour TTY defaultColourPrefs False False "" False . ppShow
file :: FilePath -> IO Blob
file :: MonadIO m => FilePath -> m Blob
file path = Files.readFile path (languageForFilePath path)
diffWithParser :: (HasField fields Data.Span.Span,
HasField fields Range,
Error :< fs,
Context :< fs,
Declaration.Method :< fs,
Declaration.Function :< fs,
Empty :< fs,
Apply Eq1 fs, Apply Show1 fs,
Apply Traversable fs, Apply Functor fs,
Apply Foldable fs, Apply Diffable fs,
Apply GAlign fs)
Eq1 syntax, Show1 syntax,
Traversable syntax, Functor syntax,
Foldable syntax, Diffable syntax,
GAlign syntax, HasDeclaration syntax)
=>
Parser (Term (Data.Union.Union fs) (Record fields))
Parser (Term syntax (Record fields))
-> Both Blob
-> Task (Diff (Union fs) (Record (Maybe Declaration ': fields)) (Record (Maybe Declaration ': fields)))
-> Task (Diff syntax (Record (Maybe Declaration ': fields)) (Record (Maybe Declaration ': fields)))
diffWithParser parser = run (\ blob -> parse parser blob >>= decorate (declarationAlgebra blob))
where
run parse sourceBlobs = distributeFor sourceBlobs parse >>= runBothWith (diffTermPair sourceBlobs diffTerms)

View File

@ -20,8 +20,7 @@ import Data.Term
import Data.Text (Text, pack)
import Language
import qualified Language.Go as Go
import qualified Language.Ruby as Ruby
import qualified Language.Ruby as TypeScript
import qualified Language.TypeScript as TypeScript
import Foreign
import Foreign.C.String (peekCString)
import Foreign.Marshal.Array (allocaArray)
@ -30,7 +29,6 @@ import qualified TreeSitter.Document as TS
import qualified TreeSitter.Node as TS
import qualified TreeSitter.Language as TS
import qualified TreeSitter.Go as TS
import qualified TreeSitter.Ruby as TS
import qualified TreeSitter.TypeScript as TS
import Info
@ -114,7 +112,6 @@ assignTerm language source annotation children allChildren =
where assignTermByLanguage :: Source -> Category -> [ Term S.Syntax (Record DefaultFields) ] -> Maybe (S.Syntax (Term S.Syntax (Record DefaultFields)))
assignTermByLanguage = case languageForTSLanguage language of
Just Language.Go -> Go.termAssignment
Just Ruby -> Ruby.termAssignment
Just TypeScript -> TypeScript.termAssignment
_ -> \ _ _ _ -> Nothing
@ -192,14 +189,13 @@ categoryForLanguageProductionName = withDefaults . byLanguage
s -> productionMap s
byLanguage language = case languageForTSLanguage language of
Just Ruby -> Ruby.categoryForRubyName
Just Language.Go -> Go.categoryForGoName
Just Language.TypeScript -> TypeScript.categoryForTypeScriptName
_ -> Other
languageForTSLanguage :: Ptr TS.Language -> Maybe Language
languageForTSLanguage = flip lookup
[ (TS.tree_sitter_go, Language.Go)
, (TS.tree_sitter_ruby, Ruby)
, (TS.tree_sitter_typescript, TypeScript)
]

83
test/Semantic/StatSpec.hs Normal file
View File

@ -0,0 +1,83 @@
module Semantic.StatSpec where
import Semantic.Stat
import Test.Hspec hiding (shouldBe, shouldNotBe, shouldThrow, errorCall)
import Test.Hspec.Expectations.Pretty
import System.Environment
import Control.Exception
import Network.Socket hiding (recv)
import Network.Socket.ByteString
withSocketPair :: ((Socket, Socket) -> IO c) -> IO c
withSocketPair = bracket create release
where create = socketPair AF_UNIX Datagram defaultProtocol
release (client, server) = close client >> close server
withEnvironment :: String -> String -> (() -> IO ()) -> IO ()
withEnvironment key value = bracket (setEnv key value) (const (unsetEnv key))
-- NOTE: These cannot easily run in parallel because we test things like
-- setting/unsetting the environment.
spec :: Spec
spec = do
describe "defaultStatsClient" $ do
it "sets appropriate defaults" $ do
StatsClient{..} <- defaultStatsClient
statsClientNamespace `shouldBe` "semantic"
statsClientUDPHost `shouldBe` "127.0.0.1"
statsClientUDPPort `shouldBe` "28125"
around (withEnvironment "STATS_ADDR" "localhost:8125") $
it "takes STATS_ADDR from environment" $ do
StatsClient{..} <- defaultStatsClient
statsClientUDPHost `shouldBe` "localhost"
statsClientUDPPort `shouldBe` "8125"
around (withEnvironment "STATS_ADDR" "localhost") $
it "handles STATS_ADDR with just hostname" $ do
StatsClient{..} <- defaultStatsClient
statsClientUDPHost `shouldBe` "localhost"
statsClientUDPPort `shouldBe` "28125"
around (withEnvironment "DOGSTATSD_HOST" "0.0.0.0") $
it "takes DOGSTATSD_HOST from environment" $ do
StatsClient{..} <- defaultStatsClient
statsClientUDPHost `shouldBe` "0.0.0.0"
statsClientUDPPort `shouldBe` "28125"
describe "renderDatagram" $ do
let key = "app.metric"
describe "counters" $ do
it "renders increment" $
renderDatagram "" (increment key []) `shouldBe` "app.metric:1|c"
it "renders decrement" $
renderDatagram "" (decrement key []) `shouldBe` "app.metric:-1|c"
it "renders count" $
renderDatagram "" (count key 8 []) `shouldBe` "app.metric:8|c"
it "renders statsClientNamespace" $
renderDatagram "pre" (increment key []) `shouldBe` "pre.app.metric:1|c"
describe "tags" $ do
it "renders a tag" $ do
let inc = increment key [("key", "value")]
renderDatagram "" inc `shouldBe` "app.metric:1|c|#key:value"
it "renders a tag without value" $ do
let inc = increment key [("a", "")]
renderDatagram "" inc `shouldBe` "app.metric:1|c|#a"
it "renders tags" $ do
let inc = increment key [("key", "value"), ("a", "true")]
renderDatagram "" inc `shouldBe` "app.metric:1|c|#key:value,a:true"
it "renders tags without value" $ do
let inc = increment key [("key", "value"), ("a", "")]
renderDatagram "" inc `shouldBe` "app.metric:1|c|#key:value,a"
describe "sendStat" $
it "delivers datagram" $ do
client@StatsClient{..} <- defaultStatsClient
withSocketPair $ \(clientSoc, serverSoc) -> do
sendStat client { statsClientUDPSocket = clientSoc } (increment "app.metric" [])
info <- recv serverSoc 1024
info `shouldBe` "semantic.app.metric:1|c"

View File

@ -62,9 +62,6 @@ diffFixtures =
patchOutput = "diff --git a/test/fixtures/ruby/method-declaration.A.rb b/test/fixtures/ruby/method-declaration.B.rb\nindex 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644\n--- a/test/fixtures/ruby/method-declaration.A.rb\n+++ b/test/fixtures/ruby/method-declaration.B.rb\n@@ -1,3 +1,4 @@\n-def foo\n+def bar(a)\n+ baz\n end\n\n"
jsonOutput = "{\"diff\":{\"merge\":{\"after\":{\"sourceRange\":[0,21],\"sourceSpan\":{\"start\":[1,1],\"end\":[4,1]}},\"children\":[{\"merge\":{\"after\":{\"sourceRange\":[0,20],\"sourceSpan\":{\"start\":[1,1],\"end\":[3,4]}},\"children\":[{\"merge\":{\"after\":{\"sourceRange\":[0,3],\"sourceSpan\":{\"start\":[1,1],\"end\":[1,4]}},\"children\":[],\"before\":{\"sourceRange\":[0,3],\"sourceSpan\":{\"start\":[1,1],\"end\":[1,4]}}}},{\"patch\":{\"replace\":[{\"children\":[],\"sourceRange\":[4,7],\"sourceSpan\":{\"start\":[1,5],\"end\":[1,8]}},{\"children\":[],\"sourceRange\":[4,7],\"sourceSpan\":{\"start\":[1,5],\"end\":[1,8]}}]}},{\"patch\":{\"insert\":{\"children\":[],\"sourceRange\":[8,9],\"sourceSpan\":{\"start\":[1,9],\"end\":[1,10]}}}},{\"merge\":{\"after\":{\"sourceRange\":[13,16],\"sourceSpan\":{\"start\":[2,3],\"end\":[2,6]}},\"children\":[{\"patch\":{\"insert\":{\"children\":[],\"sourceRange\":[13,16],\"sourceSpan\":{\"start\":[2,3],\"end\":[2,6]}}}}],\"before\":{\"sourceRange\":[8,11],\"sourceSpan\":{\"start\":[2,1],\"end\":[2,4]}}}}],\"before\":{\"sourceRange\":[0,11],\"sourceSpan\":{\"start\":[1,1],\"end\":[2,4]}}}}],\"before\":{\"sourceRange\":[0,12],\"sourceSpan\":{\"start\":[1,1],\"end\":[3,1]}}}},\"oids\":[\"0000000000000000000000000000000000000000\",\"0000000000000000000000000000000000000000\"],\"paths\":[\"test/fixtures/ruby/method-declaration.A.rb\",\"test/fixtures/ruby/method-declaration.B.rb\"]}\n"
jsonOutput = "{\"diff\":{\"merge\":{\"after\":{\"sourceRange\":[0,21],\"sourceSpan\":{\"start\":[1,1],\"end\":[4,1]}},\"children\":[{\"merge\":{\"after\":{\"sourceRange\":[0,20],\"sourceSpan\":{\"start\":[1,1],\"end\":[3,4]}},\"children\":[{\"merge\":{\"after\":{\"sourceRange\":[0,0],\"sourceSpan\":{\"start\":[1,1],\"end\":[1,1]}},\"children\":[],\"before\":{\"sourceRange\":[0,0],\"sourceSpan\":{\"start\":[1,1],\"end\":[1,1]}}}},{\"patch\":{\"replace\":[{\"children\":[],\"sourceRange\":[4,7],\"sourceSpan\":{\"start\":[1,5],\"end\":[1,8]}},{\"children\":[],\"sourceRange\":[4,7],\"sourceSpan\":{\"start\":[1,5],\"end\":[1,8]}}]}},{\"patch\":{\"insert\":{\"children\":[],\"sourceRange\":[8,9],\"sourceSpan\":{\"start\":[1,9],\"end\":[1,10]}}}},{\"merge\":{\"after\":{\"sourceRange\":[13,16],\"sourceSpan\":{\"start\":[2,3],\"end\":[2,6]}},\"children\":[{\"patch\":{\"insert\":{\"children\":[],\"sourceRange\":[13,16],\"sourceSpan\":{\"start\":[2,3],\"end\":[2,6]}}}}],\"before\":{\"sourceRange\":[8,11],\"sourceSpan\":{\"start\":[2,1],\"end\":[2,4]}}}}],\"before\":{\"sourceRange\":[0,11],\"sourceSpan\":{\"start\":[1,1],\"end\":[2,4]}}}}],\"before\":{\"sourceRange\":[0,12],\"sourceSpan\":{\"start\":[1,1],\"end\":[3,1]}}}},\"oids\":[\"0000000000000000000000000000000000000000\",\"0000000000000000000000000000000000000000\"],\"paths\":[\"test/fixtures/ruby/method-declaration.A.rb\",\"test/fixtures/ruby/method-declaration.B.rb\"]}\n"
sExpressionOutput = "(Program\n (Method\n (Empty)\n { (Identifier)\n ->(Identifier) }\n {+(Identifier)+}\n (\n {+(Identifier)+})))\n"
tocOutput = "{\"changes\":{\"test/fixtures/ruby/method-declaration.A.rb -> test/fixtures/ruby/method-declaration.B.rb\":[{\"span\":{\"start\":[1,1],\"end\":[3,4]},\"category\":\"Method\",\"term\":\"bar\",\"changeType\":\"modified\"}]},\"errors\":{}}\n"
repo :: FilePath
repo = "test/fixtures/git/examples/all-languages.git"

View File

@ -16,10 +16,12 @@ import qualified TOCSpec
import qualified IntegrationSpec
import qualified SemanticCmdLineSpec
import qualified SemanticSpec
import qualified Semantic.StatSpec
import Test.Hspec
main :: IO ()
main = hspec $ do
describe "Semantic.Stat" Semantic.StatSpec.spec
parallel $ do
describe "Alignment" AlignmentSpec.spec
describe "Command" CommandSpec.spec

View File

@ -88,7 +88,7 @@ spec = parallel $ do
it "summarizes Go methods with receivers with special formatting" $ do
sourceBlobs <- blobsForPaths (both "go/method-with-receiver.A.go" "go/method-with-receiver.B.go")
let Just goParser = parserForLanguage Go
let Just goParser = syntaxParserForLanguage Go
diff <- runTask $ distributeFor sourceBlobs (\ blob -> parse goParser blob >>= decorate (syntaxDeclarationAlgebra blob)) >>= runBothWith (diffTermPair sourceBlobs diffSyntaxTerms)
diffTOC diff `shouldBe`
[ JSONSummary "Method" "(*apiClient) CheckAuth" (sourceSpanBetween (3,1) (3,101)) "added" ]

View File

@ -4,18 +4,21 @@
(Empty)
(Empty)
{-(RequiredParameter
{-(Empty)-}
{-(Empty)-}
{-(Empty)-}
{-(Assignment
{-(Identifier)-}
{-(Empty)-})-})-}
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
{+(RequiredParameter
{+(Empty)+}
{+(Empty)+}
{+(Empty)+}
{+(Assignment

View File

@ -4,6 +4,7 @@
(Empty)
(Empty)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
@ -11,6 +12,7 @@
->(Identifier) }
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -4,12 +4,14 @@
(Empty)
(Empty)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -4,12 +4,14 @@
(Empty)
(Empty)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -4,12 +4,14 @@
(Empty)
(Empty)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -4,12 +4,14 @@
(Empty)
(Empty)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -4,12 +4,14 @@
(Empty)
(Empty)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -4,12 +4,14 @@
(Empty)
(Empty)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -9,6 +9,7 @@
(Empty)
(Empty)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -9,6 +9,7 @@
(Empty)
(Empty)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -8,6 +8,7 @@
(Empty)
(Empty)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -8,6 +8,7 @@
(Empty)
(Empty)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -9,6 +9,7 @@
{+(Empty)+}
{+(Identifier)+}
{+(RequiredParameter
{+(Empty)+}
{+(Empty)+}
{+(Empty)+}
{+(Assignment
@ -25,6 +26,7 @@
{+(Empty)+}
{+(Identifier)+}
{+(RequiredParameter
{+(Empty)+}
{+(Empty)+}
{+(Empty)+}
{+(Assignment
@ -41,6 +43,7 @@
{+(Empty)+}
{+(Identifier)+}
{+(RequiredParameter
{+(Empty)+}
{+(Empty)+}
{+(Empty)+}
{+(Assignment
@ -63,6 +66,7 @@
{-(Empty)-}
{-(Identifier)-}
{-(RequiredParameter
{-(Empty)-}
{-(Empty)-}
{-(Empty)-}
{-(Assignment
@ -79,6 +83,7 @@
{-(Empty)-}
{-(Identifier)-}
{-(RequiredParameter
{-(Empty)-}
{-(Empty)-}
{-(Empty)-}
{-(Assignment
@ -95,6 +100,7 @@
{-(Empty)-}
{-(Identifier)-}
{-(RequiredParameter
{-(Empty)-}
{-(Empty)-}
{-(Empty)-}
{-(Assignment

View File

@ -16,6 +16,7 @@
{ (Identifier)
->(Identifier) }
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
@ -33,6 +34,7 @@
{ (Identifier)
->(Identifier) }
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
@ -50,6 +52,7 @@
{ (Identifier)
->(Identifier) }
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -15,6 +15,7 @@
(Empty)
(Identifier)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
@ -31,6 +32,7 @@
(Empty)
(Identifier)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
@ -47,6 +49,7 @@
(Empty)
(Identifier)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -9,6 +9,7 @@
(Empty)
(Identifier)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
@ -25,6 +26,7 @@
(Empty)
(Identifier)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
@ -41,6 +43,7 @@
(Empty)
(Identifier)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -9,18 +9,21 @@
(Empty)
(Empty)
{-(RequiredParameter
{-(Empty)-}
{-(Empty)-}
{-(Empty)-}
{-(Assignment
{-(Identifier)-}
{-(Empty)-})-})-}
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
{+(RequiredParameter
{+(Empty)+}
{+(Empty)+}
{+(Empty)+}
{+(Assignment

View File

@ -9,6 +9,7 @@
(Empty)
(Empty)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
@ -16,6 +17,7 @@
->(Identifier) }
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -8,12 +8,14 @@
(Empty)
(Empty)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -8,12 +8,14 @@
(Empty)
(Empty)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -4,12 +4,14 @@
(Empty)
(Empty)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -4,12 +4,14 @@
(Empty)
(Empty)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -4,12 +4,14 @@
(Empty)
(Empty)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -4,12 +4,14 @@
(Empty)
(Empty)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -5,12 +5,14 @@
{ (Identifier)
->(Identifier) }
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -5,12 +5,14 @@
{ (Identifier)
->(Identifier) }
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -4,12 +4,14 @@
(Empty)
(Identifier)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -4,12 +4,14 @@
(Empty)
(Identifier)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -5,12 +5,14 @@
{ (Identifier)
->(Identifier) }
{-(RequiredParameter
{-(Empty)-}
{-(Empty)-}
{-(Empty)-}
{-(Assignment
{-(Identifier)-}
{-(Empty)-})-})-}
{-(RequiredParameter
{-(Empty)-}
{-(Empty)-}
{-(Empty)-}
{-(Assignment

View File

@ -5,12 +5,14 @@
{ (Identifier)
->(Identifier) }
{+(RequiredParameter
{+(Empty)+}
{+(Empty)+}
{+(Empty)+}
{+(Assignment
{+(Identifier)+}
{+(Empty)+})+})+}
{+(RequiredParameter
{+(Empty)+}
{+(Empty)+}
{+(Empty)+}
{+(Assignment

View File

@ -4,12 +4,14 @@
(Empty)
(Identifier)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -4,12 +4,14 @@
(Empty)
(Identifier)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -4,12 +4,14 @@
(Empty)
(Identifier)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -4,12 +4,14 @@
(Empty)
(Identifier)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -4,12 +4,14 @@
(Empty)
(Identifier)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -4,12 +4,14 @@
(Empty)
(Identifier)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
@ -21,12 +23,14 @@
(Empty)
(Identifier)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -4,12 +4,14 @@
(Empty)
(Identifier)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
@ -21,12 +23,14 @@
(Empty)
(Identifier)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -4,12 +4,14 @@
(Empty)
(Identifier)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
@ -21,12 +23,14 @@
(Empty)
(Identifier)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -4,12 +4,14 @@
(Empty)
(Identifier)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
@ -21,12 +23,14 @@
(Empty)
(Identifier)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -9,12 +9,14 @@
{ (Identifier)
->(Identifier) }
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -9,12 +9,14 @@
{ (Identifier)
->(Identifier) }
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -8,12 +8,14 @@
(Empty)
(Identifier)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -8,12 +8,14 @@
(Empty)
(Identifier)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -1,13 +1 @@
(Program
(Function
(Empty)
(Empty)
(Identifier)
(
(VariableDeclaration
(Assignment
(Empty)
(Identifier)
(Float)))
(Yield
(Identifier)))))
(Program(Function(Empty)(Empty)(Identifier)((VariableDeclaration(Assignment(Empty)(Identifier)(Float)))(Yield(Identifier)))))

View File

@ -1,875 +0,0 @@
var jQuery = (function() {
// Define a local copy of jQuery
var jQuery = function( selector, context ) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init( selector, context );
},
// Map over jQuery in case of overwrite
_jQuery = window.jQuery,
// Map over the $ in case of overwrite
_$ = window.$,
// A central reference to the root jQuery(document)
rootjQuery,
// A simple way to check for HTML strings or ID strings
// (both of which we optimize for)
quickExpr = /^(?:[^<]*(<[\w\W]+>)[^>]*$|#([\w\-]+)$)/,
// Is it a simple selector
isSimple = /^.[^:#\[\.,]*$/,
// Check if a string has a non-whitespace character in it
rnotwhite = /\S/,
rwhite = /\s/,
// Used for trimming whitespace
trimLeft = /^\s+/,
trimRight = /\s+$/,
// Check for non-word characters
rnonword = /\W/,
// Match a standalone tag
rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/,
// JSON RegExp
rvalidchars = /^[\],:{}\s]*$/,
rvalidescape = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,
rvalidtokens = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,
rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g,
// Useragent RegExp
rwebkit = /(webkit)[ \/]([\w.]+)/,
ropera = /(opera)(?:.*version)?[ \/]([\w.]+)/,
rmsie = /(msie) ([\w.]+)/,
rmozilla = /(mozilla)(?:.*? rv:([\w.]+))?/,
// Keep a UserAgent string for use with jQuery.browser
userAgent = navigator.userAgent,
// For matching the engine and version of the browser
browserMatch,
// Has the ready events already been bound?
readyBound = false,
// The functions to execute on DOM ready
readyList = [],
// The ready event handler
DOMContentLoaded,
// Save a reference to some core methods
toString = Object.prototype.toString,
hasOwn = Object.prototype.hasOwnProperty,
push = Array.prototype.push,
slice = Array.prototype.slice,
trim = String.prototype.trim,
indexOf = Array.prototype.indexOf,
// [[Class]] -> type pairs
class2type = {};
jQuery.fn = jQuery.prototype = {
init: function( selector, context ) {
var match, elem, ret, doc;
// Handle $(""), $(null), or $(undefined)
if ( !selector ) {
return this;
}
// Handle $(DOMElement)
if ( selector.nodeType ) {
this.context = this[0] = selector;
this.length = 1;
return this;
}
// The body element only exists once, optimize finding it
if ( selector === "body" && !context ) {
this.context = document;
this[0] = document.body;
this.selector = "body";
this.length = 1;
return this;
}
// Handle HTML strings
if ( typeof selector === "string" ) {
// Are we dealing with HTML string or an ID?
match = quickExpr.exec( selector );
// Verify a match, and that no context was specified for #id
if ( match && (match[1] || !context) ) {
// HANDLE: $(html) -> $(array)
if ( match[1] ) {
doc = (context ? context.ownerDocument || context : document);
// If a single string is passed in and it's a single tag
// just do a createElement and skip the rest
ret = rsingleTag.exec( selector );
if ( ret ) {
if ( jQuery.isPlainObject( context ) ) {
selector = [ document.createElement( ret[1] ) ];
jQuery.fn.attr.call( selector, context, true );
} else {
selector = [ doc.createElement( ret[1] ) ];
}
} else {
ret = jQuery.buildFragment( [ match[1] ], [ doc ] );
selector = (ret.cacheable ? ret.fragment.cloneNode(true) : ret.fragment).childNodes;
}
return jQuery.merge( this, selector );
// HANDLE: $("#id")
} else {
elem = document.getElementById( match[2] );
// Check parentNode to catch when Blackberry 4.6 returns
// nodes that are no longer in the document #6963
if ( elem && elem.parentNode ) {
// Handle the case where IE and Opera return items
// by name instead of ID
if ( elem.id !== match[2] ) {
return rootjQuery.find( selector );
}
// Otherwise, we inject the element directly into the jQuery object
this.length = 1;
this[0] = elem;
}
this.context = document;
this.selector = selector;
return this;
}
// HANDLE: $("TAG")
} else if ( !context && !rnonword.test( selector ) ) {
this.selector = selector;
this.context = document;
selector = document.getElementsByTagName( selector );
return jQuery.merge( this, selector );
// HANDLE: $(expr, $(...))
} else if ( !context || context.jquery ) {
return (context || rootjQuery).find( selector );
// HANDLE: $(expr, context)
// (which is just equivalent to: $(context).find(expr)
} else {
return jQuery( context ).find( selector );
}
// HANDLE: $(function)
// Shortcut for document ready
} else if ( jQuery.isFunction( selector ) ) {
return rootjQuery.ready( selector );
}
if (selector.selector !== undefined) {
this.selector = selector.selector;
this.context = selector.context;
}
return jQuery.makeArray( selector, this );
},
// Start with an empty selector
selector: "",
// The current version of jQuery being used
jquery: "@VERSION",
// The default length of a jQuery object is 0
length: 0,
// The number of elements contained in the matched element set
size: function() {
return this.length;
},
toArray: function() {
return slice.call( this, 0 );
},
// Get the Nth element in the matched element set OR
// Get the whole matched element set as a clean array
get: function( num ) {
return num == null ?
// Return a 'clean' array
this.toArray() :
// Return just the object
( num < 0 ? this.slice(num)[ 0 ] : this[ num ] );
},
// Take an array of elements and push it onto the stack
// (returning the new matched element set)
pushStack: function( elems, name, selector ) {
// Build a new jQuery matched element set
var ret = jQuery();
if ( jQuery.isArray( elems ) ) {
push.apply( ret, elems );
} else {
jQuery.merge( ret, elems );
}
// Add the old object onto the stack (as a reference)
ret.prevObject = this;
ret.context = this.context;
if ( name === "find" ) {
ret.selector = this.selector + (this.selector ? " " : "") + selector;
} else if ( name ) {
ret.selector = this.selector + "." + name + "(" + selector + ")";
}
// Return the newly-formed element set
return ret;
},
// Execute a callback for every element in the matched set.
// (You can seed the arguments with an array of args, but this is
// only used internally.)
each: function( callback, args ) {
return jQuery.each( this, callback, args );
},
ready: function( fn ) {
// Attach the listeners
jQuery.bindReady();
// If the DOM is already ready
if ( jQuery.isReady ) {
// Execute the function immediately
fn.call( document, jQuery );
// Otherwise, remember the function for later
} else if ( readyList ) {
// Add the function to the wait list
readyList.push( fn );
}
return this;
},
eq: function( i ) {
return i === -1 ?
this.slice( i ) :
this.slice( i, +i + 1 );
},
first: function() {
return this.eq( 0 );
},
last: function() {
return this.eq( -1 );
},
slice: function() {
return this.pushStack( slice.apply( this, arguments ),
"slice", slice.call(arguments).join(",") );
},
map: function( callback ) {
return this.pushStack( jQuery.map(this, function( elem, i ) {
return callback.call( elem, i, elem );
}));
},
end: function() {
return this.prevObject || jQuery(null);
},
// For internal use only.
// Behaves like an Array's method, not like a jQuery method.
push: push,
sort: [].sort,
splice: [].splice
};
// Give the init function the jQuery prototype for later instantiation
jQuery.fn.init.prototype = jQuery.fn;
jQuery.extend = jQuery.fn.extend = function() {
// copy reference to target object
var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy, copyIsArray;
// Handle a deep copy situation
if ( typeof target === "boolean" ) {
deep = target;
target = arguments[1] || {};
// skip the boolean and the target
i = 2;
}
// Handle case when target is a string or something (possible in deep copy)
if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
target = {};
}
// extend jQuery itself if only one argument is passed
if ( length === i ) {
target = this;
--i;
}
for ( ; i < length; i++ ) {
// Only deal with non-null/undefined values
if ( (options = arguments[ i ]) != null ) {
// Extend the base object
for ( name in options ) {
src = target[ name ];
copy = options[ name ];
// Prevent never-ending loop
if ( target === copy ) {
continue;
}
// Recurse if we're merging plain objects or arrays
if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) {
if ( copyIsArray ) {
copyIsArray = false;
clone = src && jQuery.isArray(src) ? src : [];
} else {
clone = src && jQuery.isPlainObject(src) ? src : {};
}
// Never move original objects, clone them
target[ name ] = jQuery.extend( deep, clone, copy );
// Don't bring in undefined values
} else if ( copy !== undefined ) {
target[ name ] = copy;
}
}
}
}
// Return the modified object
return target;
};
jQuery.extend({
noConflict: function( deep ) {
window.$ = _$;
if ( deep ) {
window.jQuery = _jQuery;
}
return jQuery;
},
// Is the DOM ready to be used? Set to true once it occurs.
isReady: false,
// A counter to track how many items to wait for before
// the ready event fires. See #6781
readyWait: 1,
// Handle when the DOM is ready
ready: function( wait ) {
// A third-party is pushing the ready event forwards
if ( wait === true ) {
jQuery.readyWait--;
}
// Make sure that the DOM is not already loaded
if ( !jQuery.readyWait || (wait !== true && !jQuery.isReady) ) {
// Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
if ( !document.body ) {
return setTimeout( jQuery.ready, 13 );
}
// Remember that the DOM is ready
jQuery.isReady = true;
// If a normal DOM Ready event fired, decrement, and wait if need be
if ( wait !== true && --jQuery.readyWait > 0 ) {
return;
}
// If there are functions bound, to execute
if ( readyList ) {
// Execute all of them
var fn, i = 0;
while ( (fn = readyList[ i++ ]) ) {
fn.call( document, jQuery );
}
// Reset the list of functions
readyList = null;
}
// Trigger any bound ready events
if ( jQuery.fn.triggerHandler ) {
jQuery( document ).triggerHandler( "ready" );
}
}
},
bindReady: function() {
if ( readyBound ) {
return;
}
readyBound = true;
// Catch cases where $(document).ready() is called after the
// browser event has already occurred.
if ( document.readyState === "complete" ) {
return jQuery.ready();
}
// Mozilla, Opera and webkit nightlies currently support this event
if ( document.addEventListener ) {
// Use the handy event callback
document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
// A fallback to window.onload, that will always work
window.addEventListener( "load", jQuery.ready, false );
// If IE event model is used
} else if ( document.attachEvent ) {
// ensure firing before onload,
// maybe late but safe also for iframes
document.attachEvent("onreadystatechange", DOMContentLoaded);
// A fallback to window.onload, that will always work
window.attachEvent( "onload", jQuery.ready );
// If IE and not a frame
// continually check to see if the document is ready
var toplevel = false;
try {
toplevel = window.frameElement == null;
} catch(e) {}
if ( document.documentElement.doScroll && toplevel ) {
doScrollCheck();
}
}
},
// See test/unit/core.js for details concerning isFunction.
// Since version 1.3, DOM methods and functions like alert
// aren't supported. They return false on IE (#2968).
isFunction: function( obj ) {
return jQuery.type(obj) === "function";
},
isArray: Array.isArray || function( obj ) {
return jQuery.type(obj) === "array";
},
// A crude way of determining if an object is a window
isWindow: function( obj ) {
return obj && typeof obj === "object" && "setInterval" in obj;
},
type: function( obj ) {
return obj == null ?
String( obj ) :
class2type[ toString.call(obj) ] || "object";
},
isPlainObject: function( obj ) {
// Must be an Object.
// Because of IE, we also have to check the presence of the constructor property.
// Make sure that DOM nodes and window objects don't pass through, as well
if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
return false;
}
// Not own constructor property must be Object
if ( obj.constructor &&
!hasOwn.call(obj, "constructor") &&
!hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
return false;
}
// Own properties are enumerated firstly, so to speed up,
// if last one is own, then all properties are own.
var key;
for ( key in obj ) {}
return key === undefined || hasOwn.call( obj, key );
},
isEmptyObject: function( obj ) {
for ( var name in obj ) {
return false;
}
return true;
},
error: function( msg ) {
throw msg;
},
parseJSON: function( data ) {
if ( typeof data !== "string" || !data ) {
return null;
}
// Make sure leading/trailing whitespace is removed (IE can't handle it)
data = jQuery.trim( data );
// Make sure the incoming data is actual JSON
// Logic borrowed from http://json.org/json2.js
if ( rvalidchars.test(data.replace(rvalidescape, "@")
.replace(rvalidtokens, "]")
.replace(rvalidbraces, "")) ) {
// Try to use the native JSON parser first
return window.JSON && window.JSON.parse ?
window.JSON.parse( data ) :
(new Function("return " + data))();
} else {
jQuery.error( "Invalid JSON: " + data );
}
},
noop: function() {},
// Evalulates a script in a global context
globalEval: function( data ) {
if ( data && rnotwhite.test(data) ) {
// Inspired by code by Andrea Giammarchi
// http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
var head = document.getElementsByTagName("head")[0] || document.documentElement,
script = document.createElement("script");
script.type = "text/javascript";
if ( jQuery.support.scriptEval ) {
script.appendChild( document.createTextNode( data ) );
} else {
script.text = data;
}
// Use insertBefore instead of appendChild to circumvent an IE6 bug.
// This arises when a base node is used (#2709).
head.insertBefore( script, head.firstChild );
head.removeChild( script );
}
},
nodeName: function( elem, name ) {
return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
},
// args is for internal usage only
each: function( object, callback, args ) {
var name, i = 0,
length = object.length,
isObj = length === undefined || jQuery.isFunction(object);
if ( args ) {
if ( isObj ) {
for ( name in object ) {
if ( callback.apply( object[ name ], args ) === false ) {
break;
}
}
} else {
for ( ; i < length; ) {
if ( callback.apply( object[ i++ ], args ) === false ) {
break;
}
}
}
// A special, fast, case for the most common use of each
} else {
if ( isObj ) {
for ( name in object ) {
if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
break;
}
}
} else {
for ( var value = object[0];
i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
}
}
return object;
},
// Use native String.trim function wherever possible
trim: trim ?
function( text ) {
return text == null ?
"" :
trim.call( text );
} :
// Otherwise use our own trimming functionality
function( text ) {
return text == null ?
"" :
text.toString().replace( trimLeft, "" ).replace( trimRight, "" );
},
// results is for internal usage only
makeArray: function( array, results ) {
var ret = results || [];
if ( array != null ) {
// The window, strings (and functions) also have 'length'
// The extra typeof function check is to prevent crashes
// in Safari 2 (See: #3039)
// Tweaked logic slightly to handle Blackberry 4.7 RegExp issues #6930
var type = jQuery.type(array);
if ( array.length == null || type === "string" || type === "function" || type === "regexp" || jQuery.isWindow( array ) ) {
push.call( ret, array );
} else {
jQuery.merge( ret, array );
}
}
return ret;
},
inArray: function( elem, array ) {
if ( array.indexOf ) {
return array.indexOf( elem );
}
for ( var i = 0, length = array.length; i < length; i++ ) {
if ( array[ i ] === elem ) {
return i;
}
}
return -1;
},
merge: function( first, second ) {
var i = first.length, j = 0;
if ( typeof second.length === "number" ) {
for ( var l = second.length; j < l; j++ ) {
first[ i++ ] = second[ j ];
}
} else {
while ( second[j] !== undefined ) {
first[ i++ ] = second[ j++ ];
}
}
first.length = i;
return first;
},
grep: function( elems, callback, inv ) {
var ret = [], retVal;
inv = !!inv;
// Go through the array, only saving the items
// that pass the validator function
for ( var i = 0, length = elems.length; i < length; i++ ) {
retVal = !!callback( elems[ i ], i );
if ( inv !== retVal ) {
ret.push( elems[ i ] );
}
}
return ret;
},
// arg is for internal usage only
map: function( elems, callback, arg ) {
var ret = [], value;
// Go through the array, translating each of the items to their
// new value (or values).
for ( var i = 0, length = elems.length; i < length; i++ ) {
value = callback( elems[ i ], i, arg );
if ( value != null ) {
ret[ ret.length ] = value;
}
}
return ret.concat.apply( [], ret );
},
// A global GUID counter for objects
guid: 1,
proxy: function( fn, proxy, thisObject ) {
if ( arguments.length === 2 ) {
if ( typeof proxy === "string" ) {
thisObject = fn;
fn = thisObject[ proxy ];
proxy = undefined;
} else if ( proxy && !jQuery.isFunction( proxy ) ) {
thisObject = proxy;
proxy = undefined;
}
}
if ( !proxy && fn ) {
proxy = function() {
return fn.apply( thisObject || this, arguments );
};
}
// Set the guid of unique handler to the same of original handler, so it can be removed
if ( fn ) {
proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;
}
// So proxy can be declared as an argument
return proxy;
},
// Mutifunctional method to get and set values to a collection
// The value/s can be optionally by executed if its a function
access: function( elems, key, value, exec, fn, pass ) {
var length = elems.length;
// Setting many attributes
if ( typeof key === "object" ) {
for ( var k in key ) {
jQuery.access( elems, k, key[k], exec, fn, value );
}
return elems;
}
// Setting one attribute
if ( value !== undefined ) {
// Optionally, function values get executed if exec is true
exec = !pass && exec && jQuery.isFunction(value);
for ( var i = 0; i < length; i++ ) {
fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass );
}
return elems;
}
// Getting an attribute
return length ? fn( elems[0], key ) : undefined;
},
now: function() {
return (new Date()).getTime();
},
// Use of jQuery.browser is frowned upon.
// More details: http://docs.jquery.com/Utilities/jQuery.browser
uaMatch: function( ua ) {
ua = ua.toLowerCase();
var match = rwebkit.exec( ua ) ||
ropera.exec( ua ) ||
rmsie.exec( ua ) ||
ua.indexOf("compatible") < 0 && rmozilla.exec( ua ) ||
[];
return { browser: match[1] || "", version: match[2] || "0" };
},
browser: {}
});
// Populate the class2type map
jQuery.each("Boolean Number String Function Array Date RegExp Object".split(" "), function(i, name) {
class2type[ "[object " + name + "]" ] = name.toLowerCase();
});
browserMatch = jQuery.uaMatch( userAgent );
if ( browserMatch.browser ) {
jQuery.browser[ browserMatch.browser ] = true;
jQuery.browser.version = browserMatch.version;
}
// Deprecated, use jQuery.browser.webkit instead
if ( jQuery.browser.webkit ) {
jQuery.browser.safari = true;
}
if ( indexOf ) {
jQuery.inArray = function( elem, array ) {
return indexOf.call( array, elem );
};
}
// Verify that \s matches non-breaking spaces
// (IE fails on this test)
if ( !rwhite.test( "\xA0" ) ) {
trimLeft = /^[\s\xA0]+/;
trimRight = /[\s\xA0]+$/;
}
// All jQuery objects should point back to these
rootjQuery = jQuery(document);
// Cleanup functions for the document ready method
if ( document.addEventListener ) {
DOMContentLoaded = function() {
document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
jQuery.ready();
};
} else if ( document.attachEvent ) {
DOMContentLoaded = function() {
// Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
if ( document.readyState === "complete" ) {
document.detachEvent( "onreadystatechange", DOMContentLoaded );
jQuery.ready();
}
};
}
// The DOM ready check for Internet Explorer
function doScrollCheck() {
if ( jQuery.isReady ) {
return;
}
try {
// If IE is used, use the trick by Diego Perini
// http://javascript.nwbox.com/IEContentLoaded/
document.documentElement.doScroll("left");
} catch(e) {
setTimeout( doScrollCheck, 1 );
return;
}
// and execute any waiting functions
jQuery.ready();
}
// Expose jQuery to the global object
return (window.jQuery = window.$ = jQuery);
})();

View File

@ -1,863 +0,0 @@
var jQuery = (function() {
// Define a local copy of jQuery
var jQuery = function( selector, context ) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init( selector, context );
},
// Map over jQuery in case of overwrite
_jQuery = window.jQuery,
// Map over the $ in case of overwrite
_$ = window.$,
// A central reference to the root jQuery(document)
rootjQuery,
// A simple way to check for HTML strings or ID strings
// (both of which we optimize for)
quickExpr = /^(?:[^<]*(<[\w\W]+>)[^>]*$|#([\w\-]+)$)/,
// Is it a simple selector
isSimple = /^.[^:#\[\.,]*$/,
// Check if a string has a non-whitespace character in it
rnotwhite = /\S/,
rwhite = /\s/,
// Used for trimming whitespace
trimLeft = /^\s+/,
trimRight = /\s+$/,
// Check for non-word characters
rnonword = /\W/,
// Match a standalone tag
rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/,
// JSON RegExp
rvalidchars = /^[\],:{}\s]*$/,
rvalidescape = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,
rvalidtokens = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,
rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g,
// Useragent RegExp
rwebkit = /(webkit)[ \/]([\w.]+)/,
ropera = /(opera)(?:.*version)?[ \/]([\w.]+)/,
rmsie = /(msie) ([\w.]+)/,
rmozilla = /(mozilla)(?:.*? rv:([\w.]+))?/,
// Keep a UserAgent string for use with jQuery.browser
userAgent = navigator.userAgent,
// For matching the engine and version of the browser
browserMatch,
// Has the ready events already been bound?
readyBound = false,
// The functions to execute on DOM ready
readyList = [],
// The ready event handler
DOMContentLoaded,
// Save a reference to some core methods
toString = Object.prototype.toString,
hasOwn = Object.prototype.hasOwnProperty,
push = Array.prototype.push,
slice = Array.prototype.slice,
trim = String.prototype.trim,
indexOf = Array.prototype.indexOf;
jQuery.fn = jQuery.prototype = {
init: function( selector, context ) {
var match, elem, ret, doc;
// Handle $(""), $(null), or $(undefined)
if ( !selector ) {
return this;
}
// Handle $(DOMElement)
if ( selector.nodeType ) {
this.context = this[0] = selector;
this.length = 1;
return this;
}
// The body element only exists once, optimize finding it
if ( selector === "body" && !context ) {
this.context = document;
this[0] = document.body;
this.selector = "body";
this.length = 1;
return this;
}
// Handle HTML strings
if ( typeof selector === "string" ) {
// Are we dealing with HTML string or an ID?
match = quickExpr.exec( selector );
// Verify a match, and that no context was specified for #id
if ( match && (match[1] || !context) ) {
// HANDLE: $(html) -> $(array)
if ( match[1] ) {
doc = (context ? context.ownerDocument || context : document);
// If a single string is passed in and it's a single tag
// just do a createElement and skip the rest
ret = rsingleTag.exec( selector );
if ( ret ) {
if ( jQuery.isPlainObject( context ) ) {
selector = [ document.createElement( ret[1] ) ];
jQuery.fn.attr.call( selector, context, true );
} else {
selector = [ doc.createElement( ret[1] ) ];
}
} else {
ret = jQuery.buildFragment( [ match[1] ], [ doc ] );
selector = (ret.cacheable ? ret.fragment.cloneNode(true) : ret.fragment).childNodes;
}
return jQuery.merge( this, selector );
// HANDLE: $("#id")
} else {
elem = document.getElementById( match[2] );
// Check parentNode to catch when Blackberry 4.6 returns
// nodes that are no longer in the document #6963
if ( elem && elem.parentNode ) {
// Handle the case where IE and Opera return items
// by name instead of ID
if ( elem.id !== match[2] ) {
return rootjQuery.find( selector );
}
// Otherwise, we inject the element directly into the jQuery object
this.length = 1;
this[0] = elem;
}
this.context = document;
this.selector = selector;
return this;
}
// HANDLE: $("TAG")
} else if ( !context && !rnonword.test( selector ) ) {
this.selector = selector;
this.context = document;
selector = document.getElementsByTagName( selector );
return jQuery.merge( this, selector );
// HANDLE: $(expr, $(...))
} else if ( !context || context.jquery ) {
return (context || rootjQuery).find( selector );
// HANDLE: $(expr, context)
// (which is just equivalent to: $(context).find(expr)
} else {
return jQuery( context ).find( selector );
}
// HANDLE: $(function)
// Shortcut for document ready
} else if ( jQuery.isFunction( selector ) ) {
return rootjQuery.ready( selector );
}
if (selector.selector !== undefined) {
this.selector = selector.selector;
this.context = selector.context;
}
return jQuery.makeArray( selector, this );
},
// Start with an empty selector
selector: "",
// The current version of jQuery being used
jquery: "@VERSION",
// The default length of a jQuery object is 0
length: 0,
// The number of elements contained in the matched element set
size: function() {
return this.length;
},
toArray: function() {
return slice.call( this, 0 );
},
// Get the Nth element in the matched element set OR
// Get the whole matched element set as a clean array
get: function( num ) {
return num == null ?
// Return a 'clean' array
this.toArray() :
// Return just the object
( num < 0 ? this.slice(num)[ 0 ] : this[ num ] );
},
// Take an array of elements and push it onto the stack
// (returning the new matched element set)
pushStack: function( elems, name, selector ) {
// Build a new jQuery matched element set
var ret = jQuery();
if ( jQuery.isArray( elems ) ) {
push.apply( ret, elems );
} else {
jQuery.merge( ret, elems );
}
// Add the old object onto the stack (as a reference)
ret.prevObject = this;
ret.context = this.context;
if ( name === "find" ) {
ret.selector = this.selector + (this.selector ? " " : "") + selector;
} else if ( name ) {
ret.selector = this.selector + "." + name + "(" + selector + ")";
}
// Return the newly-formed element set
return ret;
},
// Execute a callback for every element in the matched set.
// (You can seed the arguments with an array of args, but this is
// only used internally.)
each: function( callback, args ) {
return jQuery.each( this, callback, args );
},
ready: function( fn ) {
// Attach the listeners
jQuery.bindReady();
// If the DOM is already ready
if ( jQuery.isReady ) {
// Execute the function immediately
fn.call( document, jQuery );
// Otherwise, remember the function for later
} else if ( readyList ) {
// Add the function to the wait list
readyList.push( fn );
}
return this;
},
eq: function( i ) {
return i === -1 ?
this.slice( i ) :
this.slice( i, +i + 1 );
},
first: function() {
return this.eq( 0 );
},
last: function() {
return this.eq( -1 );
},
slice: function() {
return this.pushStack( slice.apply( this, arguments ),
"slice", slice.call(arguments).join(",") );
},
map: function( callback ) {
return this.pushStack( jQuery.map(this, function( elem, i ) {
return callback.call( elem, i, elem );
}));
},
end: function() {
return this.prevObject || jQuery(null);
},
// For internal use only.
// Behaves like an Array's method, not like a jQuery method.
push: push,
sort: [].sort,
splice: [].splice
};
// Give the init function the jQuery prototype for later instantiation
jQuery.fn.init.prototype = jQuery.fn;
jQuery.extend = jQuery.fn.extend = function() {
// copy reference to target object
var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy;
// Handle a deep copy situation
if ( typeof target === "boolean" ) {
deep = target;
target = arguments[1] || {};
// skip the boolean and the target
i = 2;
}
// Handle case when target is a string or something (possible in deep copy)
if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
target = {};
}
// extend jQuery itself if only one argument is passed
if ( length === i ) {
target = this;
--i;
}
for ( ; i < length; i++ ) {
// Only deal with non-null/undefined values
if ( (options = arguments[ i ]) != null ) {
// Extend the base object
for ( name in options ) {
src = target[ name ];
copy = options[ name ];
// Prevent never-ending loop
if ( target === copy ) {
continue;
}
// Recurse if we're merging object literal values or arrays
if ( deep && copy && ( jQuery.isPlainObject(copy) || jQuery.isArray(copy) ) ) {
var clone = src && ( jQuery.isPlainObject(src) || jQuery.isArray(src) ) ? src
: jQuery.isArray(copy) ? [] : {};
// Never move original objects, clone them
target[ name ] = jQuery.extend( deep, clone, copy );
// Don't bring in undefined values
} else if ( copy !== undefined ) {
target[ name ] = copy;
}
}
}
}
// Return the modified object
return target;
};
jQuery.extend({
noConflict: function( deep ) {
window.$ = _$;
if ( deep ) {
window.jQuery = _jQuery;
}
return jQuery;
},
// Is the DOM ready to be used? Set to true once it occurs.
isReady: false,
// A counter to track how many items to wait for before
// the ready event fires. See #6781
readyWait: 1,
// Handle when the DOM is ready
ready: function( wait ) {
// A third-party is pushing the ready event forwards
if ( wait === true ) {
jQuery.readyWait--;
}
// Make sure that the DOM is not already loaded
if ( !jQuery.readyWait || (wait !== true && !jQuery.isReady) ) {
// Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
if ( !document.body ) {
return setTimeout( jQuery.ready, 13 );
}
// Remember that the DOM is ready
jQuery.isReady = true;
// If a normal DOM Ready event fired, decrement, and wait if need be
if ( wait !== true && --jQuery.readyWait > 0 ) {
return;
}
// If there are functions bound, to execute
if ( readyList ) {
// Execute all of them
var fn, i = 0;
while ( (fn = readyList[ i++ ]) ) {
fn.call( document, jQuery );
}
// Reset the list of functions
readyList = null;
}
// Trigger any bound ready events
if ( jQuery.fn.triggerHandler ) {
jQuery( document ).triggerHandler( "ready" );
}
}
},
bindReady: function() {
if ( readyBound ) {
return;
}
readyBound = true;
// Catch cases where $(document).ready() is called after the
// browser event has already occurred.
if ( document.readyState === "complete" ) {
return jQuery.ready();
}
// Mozilla, Opera and webkit nightlies currently support this event
if ( document.addEventListener ) {
// Use the handy event callback
document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
// A fallback to window.onload, that will always work
window.addEventListener( "load", jQuery.ready, false );
// If IE event model is used
} else if ( document.attachEvent ) {
// ensure firing before onload,
// maybe late but safe also for iframes
document.attachEvent("onreadystatechange", DOMContentLoaded);
// A fallback to window.onload, that will always work
window.attachEvent( "onload", jQuery.ready );
// If IE and not a frame
// continually check to see if the document is ready
var toplevel = false;
try {
toplevel = window.frameElement == null;
} catch(e) {}
if ( document.documentElement.doScroll && toplevel ) {
doScrollCheck();
}
}
},
// See test/unit/core.js for details concerning isFunction.
// Since version 1.3, DOM methods and functions like alert
// aren't supported. They return false on IE (#2968).
isFunction: function( obj ) {
return jQuery.type(obj) === "function";
},
isArray: Array.isArray || function( obj ) {
return jQuery.type(obj) === "array";
},
// A crude way of determining if an object is a window
isWindow: function( obj ) {
return obj && typeof obj === "object" && "setInterval" in obj;
},
type: function( obj ) {
return obj == null ?
String( obj ) :
toString.call(obj).slice(8, -1).toLowerCase();
},
isPlainObject: function( obj ) {
// Must be an Object.
// Because of IE, we also have to check the presence of the constructor property.
// Make sure that DOM nodes and window objects don't pass through, as well
if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
return false;
}
// Not own constructor property must be Object
if ( obj.constructor &&
!hasOwn.call(obj, "constructor") &&
!hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
return false;
}
// Own properties are enumerated firstly, so to speed up,
// if last one is own, then all properties are own.
var key;
for ( key in obj ) {}
return key === undefined || hasOwn.call( obj, key );
},
isEmptyObject: function( obj ) {
for ( var name in obj ) {
return false;
}
return true;
},
error: function( msg ) {
throw msg;
},
parseJSON: function( data ) {
if ( typeof data !== "string" || !data ) {
return null;
}
// Make sure leading/trailing whitespace is removed (IE can't handle it)
data = jQuery.trim( data );
// Make sure the incoming data is actual JSON
// Logic borrowed from http://json.org/json2.js
if ( rvalidchars.test(data.replace(rvalidescape, "@")
.replace(rvalidtokens, "]")
.replace(rvalidbraces, "")) ) {
// Try to use the native JSON parser first
return window.JSON && window.JSON.parse ?
window.JSON.parse( data ) :
(new Function("return " + data))();
} else {
jQuery.error( "Invalid JSON: " + data );
}
},
noop: function() {},
// Evalulates a script in a global context
globalEval: function( data ) {
if ( data && rnotwhite.test(data) ) {
// Inspired by code by Andrea Giammarchi
// http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
var head = document.getElementsByTagName("head")[0] || document.documentElement,
script = document.createElement("script");
script.type = "text/javascript";
if ( jQuery.support.scriptEval ) {
script.appendChild( document.createTextNode( data ) );
} else {
script.text = data;
}
// Use insertBefore instead of appendChild to circumvent an IE6 bug.
// This arises when a base node is used (#2709).
head.insertBefore( script, head.firstChild );
head.removeChild( script );
}
},
nodeName: function( elem, name ) {
return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
},
// args is for internal usage only
each: function( object, callback, args ) {
var name, i = 0,
length = object.length,
isObj = length === undefined || jQuery.isFunction(object);
if ( args ) {
if ( isObj ) {
for ( name in object ) {
if ( callback.apply( object[ name ], args ) === false ) {
break;
}
}
} else {
for ( ; i < length; ) {
if ( callback.apply( object[ i++ ], args ) === false ) {
break;
}
}
}
// A special, fast, case for the most common use of each
} else {
if ( isObj ) {
for ( name in object ) {
if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
break;
}
}
} else {
for ( var value = object[0];
i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
}
}
return object;
},
// Use native String.trim function wherever possible
trim: trim ?
function( text ) {
return text == null ?
"" :
trim.call( text );
} :
// Otherwise use our own trimming functionality
function( text ) {
return text == null ?
"" :
text.toString().replace( trimLeft, "" ).replace( trimRight, "" );
},
// results is for internal usage only
makeArray: function( array, results ) {
var ret = results || [];
if ( array != null ) {
// The window, strings (and functions) also have 'length'
// The extra typeof function check is to prevent crashes
// in Safari 2 (See: #3039)
// Tweaked logic slightly to handle Blackberry 4.7 RegExp issues #6930
var type = jQuery.type(array);
if ( array.length == null || type === "string" || type === "function" || type === "regexp" || jQuery.isWindow( array ) ) {
push.call( ret, array );
} else {
jQuery.merge( ret, array );
}
}
return ret;
},
inArray: function( elem, array ) {
if ( array.indexOf ) {
return array.indexOf( elem );
}
for ( var i = 0, length = array.length; i < length; i++ ) {
if ( array[ i ] === elem ) {
return i;
}
}
return -1;
},
merge: function( first, second ) {
var i = first.length, j = 0;
if ( typeof second.length === "number" ) {
for ( var l = second.length; j < l; j++ ) {
first[ i++ ] = second[ j ];
}
} else {
while ( second[j] !== undefined ) {
first[ i++ ] = second[ j++ ];
}
}
first.length = i;
return first;
},
grep: function( elems, callback, inv ) {
var ret = [], retVal;
inv = !!inv;
// Go through the array, only saving the items
// that pass the validator function
for ( var i = 0, length = elems.length; i < length; i++ ) {
retVal = !!callback( elems[ i ], i );
if ( inv !== retVal ) {
ret.push( elems[ i ] );
}
}
return ret;
},
// arg is for internal usage only
map: function( elems, callback, arg ) {
var ret = [], value;
// Go through the array, translating each of the items to their
// new value (or values).
for ( var i = 0, length = elems.length; i < length; i++ ) {
value = callback( elems[ i ], i, arg );
if ( value != null ) {
ret[ ret.length ] = value;
}
}
return ret.concat.apply( [], ret );
},
// A global GUID counter for objects
guid: 1,
proxy: function( fn, proxy, thisObject ) {
if ( arguments.length === 2 ) {
if ( typeof proxy === "string" ) {
thisObject = fn;
fn = thisObject[ proxy ];
proxy = undefined;
} else if ( proxy && !jQuery.isFunction( proxy ) ) {
thisObject = proxy;
proxy = undefined;
}
}
if ( !proxy && fn ) {
proxy = function() {
return fn.apply( thisObject || this, arguments );
};
}
// Set the guid of unique handler to the same of original handler, so it can be removed
if ( fn ) {
proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;
}
// So proxy can be declared as an argument
return proxy;
},
// Mutifunctional method to get and set values to a collection
// The value/s can be optionally by executed if its a function
access: function( elems, key, value, exec, fn, pass ) {
var length = elems.length;
// Setting many attributes
if ( typeof key === "object" ) {
for ( var k in key ) {
jQuery.access( elems, k, key[k], exec, fn, value );
}
return elems;
}
// Setting one attribute
if ( value !== undefined ) {
// Optionally, function values get executed if exec is true
exec = !pass && exec && jQuery.isFunction(value);
for ( var i = 0; i < length; i++ ) {
fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass );
}
return elems;
}
// Getting an attribute
return length ? fn( elems[0], key ) : undefined;
},
now: function() {
return (new Date()).getTime();
},
// Use of jQuery.browser is frowned upon.
// More details: http://docs.jquery.com/Utilities/jQuery.browser
uaMatch: function( ua ) {
ua = ua.toLowerCase();
var match = rwebkit.exec( ua ) ||
ropera.exec( ua ) ||
rmsie.exec( ua ) ||
ua.indexOf("compatible") < 0 && rmozilla.exec( ua ) ||
[];
return { browser: match[1] || "", version: match[2] || "0" };
},
browser: {}
});
browserMatch = jQuery.uaMatch( userAgent );
if ( browserMatch.browser ) {
jQuery.browser[ browserMatch.browser ] = true;
jQuery.browser.version = browserMatch.version;
}
// Deprecated, use jQuery.browser.webkit instead
if ( jQuery.browser.webkit ) {
jQuery.browser.safari = true;
}
if ( indexOf ) {
jQuery.inArray = function( elem, array ) {
return indexOf.call( array, elem );
};
}
// Verify that \s matches non-breaking spaces
// (IE fails on this test)
if ( !rwhite.test( "\xA0" ) ) {
trimLeft = /^[\s\xA0]+/;
trimRight = /[\s\xA0]+$/;
}
// All jQuery objects should point back to these
rootjQuery = jQuery(document);
// Cleanup functions for the document ready method
if ( document.addEventListener ) {
DOMContentLoaded = function() {
document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
jQuery.ready();
};
} else if ( document.attachEvent ) {
DOMContentLoaded = function() {
// Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
if ( document.readyState === "complete" ) {
document.detachEvent( "onreadystatechange", DOMContentLoaded );
jQuery.ready();
}
};
}
// The DOM ready check for Internet Explorer
function doScrollCheck() {
if ( jQuery.isReady ) {
return;
}
try {
// If IE is used, use the trick by Diego Perini
// http://javascript.nwbox.com/IEContentLoaded/
document.documentElement.doScroll("left");
} catch(e) {
setTimeout( doScrollCheck, 1 );
return;
}
// and execute any waiting functions
jQuery.ready();
}
// Expose jQuery to the global object
return (window.jQuery = window.$ = jQuery);
})();

View File

@ -1,177 +0,0 @@
define( [
"../core",
"../var/rnotwhite",
"../data/var/dataPriv",
"../core/init"
], function( jQuery, rnotwhite, dataPriv ) {
var rclass = /[\t\r\n\f]/g;
function getClass( elem ) {
return elem.getAttribute && elem.getAttribute( "class" ) || "";
}
jQuery.fn.extend( {
addClass: function( value ) {
var classes, elem, cur, curValue, clazz, j, finalValue,
i = 0;
if ( jQuery.isFunction( value ) ) {
return this.each( function( j ) {
jQuery( this ).addClass( value.call( this, j, getClass( this ) ) );
} );
}
if ( typeof value === "string" && value ) {
classes = value.match( rnotwhite ) || [];
while ( ( elem = this[ i++ ] ) ) {
curValue = getClass( elem );
cur = elem.nodeType === 1 &&
( " " + curValue + " " ).replace( rclass, " " );
if ( cur ) {
j = 0;
while ( ( clazz = classes[ j++ ] ) ) {
if ( cur.indexOf( " " + clazz + " " ) < 0 ) {
cur += clazz + " ";
}
}
// Only assign if different to avoid unneeded rendering.
finalValue = jQuery.trim( cur );
if ( curValue !== finalValue ) {
elem.setAttribute( "class", finalValue );
}
}
}
}
return this;
},
removeClass: function( value ) {
var classes, elem, cur, curValue, clazz, j, finalValue,
i = 0;
if ( jQuery.isFunction( value ) ) {
return this.each( function( j ) {
jQuery( this ).removeClass( value.call( this, j, getClass( this ) ) );
} );
}
if ( !arguments.length ) {
return this.attr( "class", "" );
}
if ( typeof value === "string" && value ) {
classes = value.match( rnotwhite ) || [];
while ( ( elem = this[ i++ ] ) ) {
curValue = getClass( elem );
// This expression is here for better compressibility (see addClass)
cur = elem.nodeType === 1 &&
( " " + curValue + " " ).replace( rclass, " " );
if ( cur ) {
j = 0;
while ( ( clazz = classes[ j++ ] ) ) {
// Remove *all* instances
while ( cur.indexOf( " " + clazz + " " ) > -1 ) {
cur = cur.replace( " " + clazz + " ", " " );
}
}
// Only assign if different to avoid unneeded rendering.
finalValue = jQuery.trim( cur );
if ( curValue !== finalValue ) {
elem.setAttribute( "class", finalValue );
}
}
}
}
return this;
},
toggleClass: function( value, stateVal ) {
var type = typeof value;
if ( typeof stateVal === "boolean" && type === "string" ) {
return stateVal ? this.addClass( value ) : this.removeClass( value );
}
if ( jQuery.isFunction( value ) ) {
return this.each( function( i ) {
jQuery( this ).toggleClass(
value.call( this, i, getClass( this ), stateVal ),
stateVal
);
} );
}
return this.each( function() {
var className, i, self, classNames;
if ( type === "string" ) {
// Toggle individual class names
i = 0;
self = jQuery( this );
classNames = value.match( rnotwhite ) || [];
while ( ( className = classNames[ i++ ] ) ) {
// Check each className given, space separated list
if ( self.hasClass( className ) ) {
self.removeClass( className );
} else {
self.addClass( className );
}
}
// Toggle whole class name
} else if ( value === undefined || type === "boolean" ) {
className = getClass( this );
if ( className ) {
// Store className if set
dataPriv.set( this, "__className__", className );
}
// If the element has a class name or if we're passed `false`,
// then remove the whole classname (if there was one, the above saved it).
// Otherwise bring back whatever was previously saved (if anything),
// falling back to the empty string if nothing was stored.
if ( this.setAttribute ) {
this.setAttribute( "class",
className || value === false ?
"" :
dataPriv.get( this, "__className__" ) || ""
);
}
}
} );
},
hasClass: function( selector ) {
var className, elem,
i = 0;
className = " " + selector + " ";
while ( ( elem = this[ i++ ] ) ) {
if ( elem.nodeType === 1 &&
( " " + getClass( elem ) + " " ).replace( rclass, " " )
.indexOf( className ) > -1
) {
return true;
}
}
return false;
}
} );
} );

View File

@ -1,174 +0,0 @@
define( [
"../core",
"../core/stripAndCollapse",
"../var/rnothtmlwhite",
"../data/var/dataPriv",
"../core/init"
], function( jQuery, stripAndCollapse, rnothtmlwhite, dataPriv ) {
"use strict";
function getClass( elem ) {
return elem.getAttribute && elem.getAttribute( "class" ) || "";
}
jQuery.fn.extend( {
addClass: function( value ) {
var classes, elem, cur, curValue, clazz, j, finalValue,
i = 0;
if ( jQuery.isFunction( value ) ) {
return this.each( function( j ) {
jQuery( this ).addClass( value.call( this, j, getClass( this ) ) );
} );
}
if ( typeof value === "string" && value ) {
classes = value.match( rnothtmlwhite ) || [];
while ( ( elem = this[ i++ ] ) ) {
curValue = getClass( elem );
cur = elem.nodeType === 1 && ( " " + stripAndCollapse( curValue ) + " " );
if ( cur ) {
j = 0;
while ( ( clazz = classes[ j++ ] ) ) {
if ( cur.indexOf( " " + clazz + " " ) < 0 ) {
cur += clazz + " ";
}
}
// Only assign if different to avoid unneeded rendering.
finalValue = stripAndCollapse( cur );
if ( curValue !== finalValue ) {
elem.setAttribute( "class", finalValue );
}
}
}
}
return this;
},
removeClass: function( value ) {
var classes, elem, cur, curValue, clazz, j, finalValue,
i = 0;
if ( jQuery.isFunction( value ) ) {
return this.each( function( j ) {
jQuery( this ).removeClass( value.call( this, j, getClass( this ) ) );
} );
}
if ( !arguments.length ) {
return this.attr( "class", "" );
}
if ( typeof value === "string" && value ) {
classes = value.match( rnothtmlwhite ) || [];
while ( ( elem = this[ i++ ] ) ) {
curValue = getClass( elem );
// This expression is here for better compressibility (see addClass)
cur = elem.nodeType === 1 && ( " " + stripAndCollapse( curValue ) + " " );
if ( cur ) {
j = 0;
while ( ( clazz = classes[ j++ ] ) ) {
// Remove *all* instances
while ( cur.indexOf( " " + clazz + " " ) > -1 ) {
cur = cur.replace( " " + clazz + " ", " " );
}
}
// Only assign if different to avoid unneeded rendering.
finalValue = stripAndCollapse( cur );
if ( curValue !== finalValue ) {
elem.setAttribute( "class", finalValue );
}
}
}
}
return this;
},
toggleClass: function( value, stateVal ) {
var type = typeof value;
if ( typeof stateVal === "boolean" && type === "string" ) {
return stateVal ? this.addClass( value ) : this.removeClass( value );
}
if ( jQuery.isFunction( value ) ) {
return this.each( function( i ) {
jQuery( this ).toggleClass(
value.call( this, i, getClass( this ), stateVal ),
stateVal
);
} );
}
return this.each( function() {
var className, i, self, classNames;
if ( type === "string" ) {
// Toggle individual class names
i = 0;
self = jQuery( this );
classNames = value.match( rnothtmlwhite ) || [];
while ( ( className = classNames[ i++ ] ) ) {
// Check each className given, space separated list
if ( self.hasClass( className ) ) {
self.removeClass( className );
} else {
self.addClass( className );
}
}
// Toggle whole class name
} else if ( value === undefined || type === "boolean" ) {
className = getClass( this );
if ( className ) {
// Store className if set
dataPriv.set( this, "__className__", className );
}
// If the element has a class name or if we're passed `false`,
// then remove the whole classname (if there was one, the above saved it).
// Otherwise bring back whatever was previously saved (if anything),
// falling back to the empty string if nothing was stored.
if ( this.setAttribute ) {
this.setAttribute( "class",
className || value === false ?
"" :
dataPriv.get( this, "__className__" ) || ""
);
}
}
} );
},
hasClass: function( selector ) {
var className, elem,
i = 0;
className = " " + selector + " ";
while ( ( elem = this[ i++ ] ) ) {
if ( elem.nodeType === 1 &&
( " " + stripAndCollapse( getClass( elem ) ) + " " ).indexOf( className ) > -1 ) {
return true;
}
}
return false;
}
} );
} );

View File

@ -1,56 +0,0 @@
// Use the right jQuery source on the test page (and iframes)
( function() {
/* global loadTests: false */
var path = window.location.pathname.split( "test" )[ 0 ],
QUnit = window.QUnit || parent.QUnit,
require = window.require || parent.require,
// Default to unminified jQuery for directly-opened iframes
urlParams = QUnit ?
QUnit.urlParams :
{ dev: true },
src = urlParams.dev ?
"dist/jquery.js" :
"dist/jquery.min.js";
// Define configuration parameters controlling how jQuery is loaded
if ( QUnit ) {
QUnit.config.urlConfig.push( {
id: "amd",
label: "Load with AMD",
tooltip: "Load the AMD jQuery file (and its dependencies)"
} );
QUnit.config.urlConfig.push( {
id: "dev",
label: "Load unminified",
tooltip: "Load the development (unminified) jQuery file"
} );
}
// Honor AMD loading on the main window (detected by seeing QUnit on it).
// This doesn't apply to iframes because they synchronously expect jQuery to be there.
if ( urlParams.amd && window.QUnit ) {
require.config( {
baseUrl: path
} );
src = "src/jquery";
// Include tests if specified
if ( typeof loadTests !== "undefined" ) {
require( [ src ], loadTests );
} else {
require( [ src ] );
}
// Otherwise, load synchronously
} else {
document.write( "<script id='jquery-js' src='" + path + src + "'><\x2Fscript>" );
// Synchronous-only tests (other tests are loaded from the test page)
if ( typeof loadTests !== "undefined" ) {
document.write( "<script src='" + path + "test/unit/ready.js'><\x2Fscript>" );
}
}
} )();

View File

@ -1,55 +0,0 @@
// Use the right jQuery source on the test page (and iframes)
( function() {
/* global loadTests: false */
var src,
path = window.location.pathname.split( "test" )[ 0 ],
QUnit = window.QUnit || parent.QUnit,
require = window.require || parent.require;
// iFrames won't load AMD (the iframe tests synchronously expect jQuery to be there)
QUnit.config.urlConfig.push( {
id: "amd",
label: "Load with AMD",
tooltip: "Load the AMD jQuery file (and its dependencies)"
} );
// If QUnit is on window, this is the main window
// This detection allows AMD tests to be run in an iframe
if ( QUnit.urlParams.amd && window.QUnit ) {
require.config( {
baseUrl: path
} );
src = "src/jquery";
// Include tests if specified
if ( typeof loadTests !== "undefined" ) {
require( [ src ], loadTests );
} else {
require( [ src ] );
}
return;
}
// Config parameter to use minified jQuery
QUnit.config.urlConfig.push( {
id: "dev",
label: "Load unminified",
tooltip: "Load the development (unminified) jQuery file"
} );
if ( QUnit.urlParams.dev ) {
src = "dist/jquery.js";
} else {
src = "dist/jquery.min.js";
}
// Load jQuery
document.write( "<script id='jquery-js' src='" + path + src + "'><\x2Fscript>" );
// Synchronous-only tests
// Other tests are loaded from the test page
if ( typeof loadTests !== "undefined" ) {
document.write( "<script src='" + path + "test/unit/ready.js'><\x2Fscript>" );
}
} )();

View File

@ -34,6 +34,7 @@
{-(PredefinedType)-})-}
{-(Identifier)-}
{-(RequiredParameter
{-(Empty)-}
{-(Empty)-}
{-(Annotation
{-(PredefinedType)-})-}
@ -49,6 +50,7 @@
{-(PredefinedType)-})-}
{-(Identifier)-}
{-(RequiredParameter
{-(Empty)-}
{-(Empty)-}
{-(Annotation
{-(PredefinedType)-})-}
@ -105,6 +107,7 @@
(Empty)
(Identifier)
(RequiredParameter
(Empty)
(Empty)
(Annotation
(PredefinedType))

View File

@ -23,6 +23,7 @@
{+(PredefinedType)+})+}
{+(Identifier)+}
{+(RequiredParameter
{+(Empty)+}
{+(Empty)+}
{+(Annotation
{+(PredefinedType)+})+}
@ -38,6 +39,7 @@
{+(PredefinedType)+})+}
{+(Identifier)+}
{+(RequiredParameter
{+(Empty)+}
{+(Empty)+}
{+(Annotation
{+(PredefinedType)+})+}
@ -106,6 +108,7 @@
(Empty)
(Identifier)
(RequiredParameter
(Empty)
(Empty)
(Annotation
(PredefinedType))

View File

@ -23,6 +23,7 @@
(PredefinedType))
(Identifier)
(RequiredParameter
(Empty)
(Empty)
(Annotation
(PredefinedType))
@ -38,6 +39,7 @@
(PredefinedType))
(Identifier)
(RequiredParameter
(Empty)
(Empty)
(Annotation
(PredefinedType))
@ -94,6 +96,7 @@
(Empty)
(Identifier)
(RequiredParameter
(Empty)
(Empty)
(Annotation
(PredefinedType))

View File

@ -21,6 +21,7 @@
(Empty)
(Identifier)
(RequiredParameter
(Empty)
(Empty)
(Annotation
(PredefinedType))

View File

@ -5,6 +5,7 @@
{+(Empty)+}
{+(Identifier)+}
{+(RequiredParameter
{+(Empty)+}
{+(Empty)+}
{+(Annotation
{+(PredefinedType)+})+}
@ -12,6 +13,7 @@
{+(Identifier)+}
{+(Empty)+})+})+}
{+(RequiredParameter
{+(Empty)+}
{+(Empty)+}
{+(Annotation
{+(PredefinedType)+})+}

View File

@ -7,6 +7,7 @@
{-(Empty)-}
{-(Identifier)-}
{-(RequiredParameter
{-(Empty)-}
{-(Empty)-}
{-(Annotation
{-(PredefinedType)-})-}
@ -14,6 +15,7 @@
{-(Identifier)-}
{-(Empty)-})-})-}
{-(RequiredParameter
{-(Empty)-}
{-(Empty)-}
{-(Annotation
{-(PredefinedType)-})-}

View File

@ -5,6 +5,7 @@
(Empty)
(Identifier)
(RequiredParameter
(Empty)
(Empty)
(Annotation
(PredefinedType))
@ -12,6 +13,7 @@
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Annotation
(PredefinedType))

View File

@ -4,18 +4,21 @@
(Empty)
(Empty)
{-(RequiredParameter
{-(Empty)-}
{-(Empty)-}
{-(Empty)-}
{-(Assignment
{-(Identifier)-}
{-(Empty)-})-})-}
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
{+(RequiredParameter
{+(Empty)+}
{+(Empty)+}
{+(Empty)+}
{+(Assignment

View File

@ -4,6 +4,7 @@
(Empty)
(Empty)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
@ -11,6 +12,7 @@
->(Identifier) }
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -4,12 +4,14 @@
(Empty)
(Empty)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -4,12 +4,14 @@
(Empty)
(Empty)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -4,12 +4,14 @@
(Empty)
(Empty)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -4,12 +4,14 @@
(Empty)
(Empty)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -4,12 +4,14 @@
(Empty)
(Empty)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -4,12 +4,14 @@
(Empty)
(Empty)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -9,6 +9,7 @@
(Empty)
(Empty)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -9,6 +9,7 @@
(Empty)
(Empty)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -8,6 +8,7 @@
(Empty)
(Empty)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -8,6 +8,7 @@
(Empty)
(Empty)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -17,6 +17,7 @@
{+(Empty)+}
{+(Identifier)+}
{+(RequiredParameter
{+(Empty)+}
{+(Empty)+}
{+(Empty)+}
{+(Assignment
@ -33,6 +34,7 @@
{+(Empty)+}
{+(Identifier)+}
{+(RequiredParameter
{+(Empty)+}
{+(Empty)+}
{+(Empty)+}
{+(Assignment
@ -49,6 +51,7 @@
{+(Empty)+}
{+(Identifier)+}
{+(RequiredParameter
{+(Empty)+}
{+(Empty)+}
{+(Empty)+}
{+(Assignment
@ -71,6 +74,7 @@
{-(Empty)-}
{-(Identifier)-}
{-(RequiredParameter
{-(Empty)-}
{-(Empty)-}
{-(Empty)-}
{-(Assignment
@ -87,6 +91,7 @@
{-(Empty)-}
{-(Identifier)-}
{-(RequiredParameter
{-(Empty)-}
{-(Empty)-}
{-(Empty)-}
{-(Assignment
@ -103,6 +108,7 @@
{-(Empty)-}
{-(Identifier)-}
{-(RequiredParameter
{-(Empty)-}
{-(Empty)-}
{-(Empty)-}
{-(Assignment

View File

@ -24,6 +24,7 @@
{ (Identifier)
->(Identifier) }
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
@ -41,6 +42,7 @@
{ (Identifier)
->(Identifier) }
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
@ -58,6 +60,7 @@
{ (Identifier)
->(Identifier) }
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -20,6 +20,7 @@
(Empty)
(Identifier)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
@ -36,6 +37,7 @@
(Empty)
(Identifier)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
@ -52,6 +54,7 @@
(Empty)
(Identifier)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -14,6 +14,7 @@
(Empty)
(Identifier)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
@ -30,6 +31,7 @@
(Empty)
(Identifier)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
@ -46,6 +48,7 @@
(Empty)
(Identifier)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -9,18 +9,21 @@
(Empty)
(Empty)
{-(RequiredParameter
{-(Empty)-}
{-(Empty)-}
{-(Empty)-}
{-(Assignment
{-(Identifier)-}
{-(Empty)-})-})-}
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
{+(RequiredParameter
{+(Empty)+}
{+(Empty)+}
{+(Empty)+}
{+(Assignment

View File

@ -9,6 +9,7 @@
(Empty)
(Empty)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
@ -16,6 +17,7 @@
->(Identifier) }
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -8,12 +8,14 @@
(Empty)
(Empty)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

View File

@ -8,12 +8,14 @@
(Empty)
(Empty)
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment
(Identifier)
(Empty)))
(RequiredParameter
(Empty)
(Empty)
(Empty)
(Assignment

Some files were not shown because too many files have changed in this diff Show More