Compare commits
120
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c53e001973 | ||
|
|
976e4ff711 | ||
|
|
6e64cdcbfc | ||
|
|
73e479e171 | ||
|
|
758da604cd | ||
|
|
95a0074edf | ||
|
|
32e1b114df | ||
|
|
afec2544f1 | ||
|
|
6828ec9852 | ||
|
|
b3557429eb | ||
|
|
0618e61e0b | ||
|
|
a2643cc0d5 | ||
|
|
359d5f33bd | ||
|
|
fcd7cf4ddd | ||
|
|
1f4a5d8d9f | ||
|
|
92ab193369 | ||
|
|
fc57cf702c | ||
|
|
f51eca20f5 | ||
|
|
9887cb57b2 | ||
|
|
040604b7db | ||
|
|
26c7132237 | ||
|
|
483f6f2134 | ||
|
|
c463877e17 | ||
|
|
71ab537ee9 | ||
|
|
134b062be7 | ||
|
|
3898f744dc | ||
|
|
305059ff9f | ||
|
|
5863160112 | ||
|
|
fbf7bc835b | ||
|
|
04e85e11de | ||
|
|
20e45f363c | ||
|
|
46af5238ce | ||
|
|
cf134909fd | ||
|
|
a4eea165bb | ||
|
|
25cbab056b | ||
|
|
8041ce3119 | ||
|
|
8f0c91c032 | ||
|
|
940f8c4270 | ||
|
|
5b9bb64a88 | ||
|
|
7d8d97933f | ||
|
|
f13865d7a1 | ||
|
|
49cf2e67fe | ||
|
|
cbdeddbab8 | ||
|
|
eab9c4e587 | ||
|
|
830610cef8 | ||
|
|
297a0679d1 | ||
|
|
fe33150b7f | ||
|
|
3db27b01a1 | ||
|
|
326771f4b4 | ||
|
|
9c15ad2021 | ||
|
|
84ea129f26 | ||
|
|
50ebb036c4 | ||
|
|
539156430a | ||
|
|
cc2fb20ca0 | ||
|
|
df195b3187 | ||
|
|
f6c6ba95ad | ||
|
|
d9d6f43407 | ||
|
|
51a44edc63 | ||
|
|
062a6aa6d7 | ||
|
|
4c7bede5a4 | ||
|
|
0ce437ea97 | ||
|
|
ac3f8bcc52 | ||
|
|
95dd64b0f0 | ||
|
|
4d47f8f1e1 | ||
|
|
3895bbf856 | ||
|
|
aa3ebdfe9b | ||
|
|
6f3abbac62 | ||
|
|
65ce73deb4 | ||
|
|
e718bd50b6 | ||
|
|
a3e509034b | ||
|
|
0159c0ecc5 | ||
|
|
649cac791a | ||
|
|
5c25c90657 | ||
|
|
c2593dc417 | ||
|
|
e258d20ba2 | ||
|
|
a4ff3d4670 | ||
|
|
e6a85737da | ||
|
|
0dfaebcdcb | ||
|
|
0fd7d4b30c | ||
|
|
58a9b0cfe1 | ||
|
|
da5d653de8 | ||
|
|
6e6ea50763 | ||
|
|
c5ab3b2336 | ||
|
|
328c4a13f1 | ||
|
|
5add2c2ceb | ||
|
|
520c7bb15e | ||
|
|
32e324a283 | ||
|
|
eea05ff924 | ||
|
|
c9dbe1aff8 | ||
|
|
a8c3648ca5 | ||
|
|
d895d39ba9 | ||
|
|
8fe9c8a22a | ||
|
|
d8fad1ed9b | ||
|
|
cf004c2b3a | ||
|
|
6041fd7661 | ||
|
|
52487ff812 | ||
|
|
22ffc0fdfc | ||
|
|
e3710c0d1f | ||
|
|
4f747e6fff | ||
|
|
9da3e0d373 | ||
|
|
f72c5ffac0 | ||
|
|
2cb9a6e61d | ||
|
|
22f8aed31e | ||
|
|
e98624e5a9 | ||
|
|
92654f8204 | ||
|
|
66fd702363 | ||
|
|
9e544c55dc | ||
|
|
f25cb16e6b | ||
|
|
463b175cdb | ||
|
|
dc6cf2fb58 | ||
|
|
6c8b55793a | ||
|
|
a847d2250f | ||
|
|
befb103a12 | ||
|
|
2086e59ab1 | ||
|
|
79d81f4bee | ||
|
|
c128c2029e | ||
|
|
dd0d340eba | ||
|
|
15ad640202 | ||
|
|
fd738bface | ||
|
|
2002dc1a2f |
No files matched your search
+13
@@ -0,0 +1,13 @@
|
||||
*.o
|
||||
*~
|
||||
|
||||
# Autogenerated
|
||||
*.gen.*
|
||||
|
||||
# Test binaries
|
||||
jv_test
|
||||
jv_parse
|
||||
parsertest*~
|
||||
|
||||
# Something delightfully recursive happens otherwise
|
||||
docs/content/2.download/source/*
|
||||
@@ -0,0 +1,70 @@
|
||||
jq is copyright (C) 2012 Stephen Dolan
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
"Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
|
||||
|
||||
jq's documentation (everything found under the docs/ subdirectory in
|
||||
the source tree) is licensed under the Creative Commons CC BY 3.0
|
||||
license, which can be found at:
|
||||
|
||||
http://creativecommons.org/licenses/by/3.0/
|
||||
|
||||
The documentation website includes a copy of Twitter's Boostrap and
|
||||
relies on Bonsai, Liquid templates and various other projects, look
|
||||
them up for detailed licensing conditions.
|
||||
|
||||
|
||||
|
||||
jq incorporates David M. Gay's dtoa.c and g_fmt.c, which bear the
|
||||
following notices:
|
||||
|
||||
dtoa.c:
|
||||
The author of this software is David M. Gay.
|
||||
|
||||
Copyright (c) 1991, 2000, 2001 by Lucent Technologies.
|
||||
|
||||
Permission to use, copy, modify, and distribute this software for any
|
||||
purpose without fee is hereby granted, provided that this entire notice
|
||||
is included in all copies of any software which is or includes a copy
|
||||
or modification of this software and in all copies of the supporting
|
||||
documentation for such software.
|
||||
|
||||
THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
|
||||
WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
|
||||
REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
|
||||
OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
|
||||
|
||||
g_fmt.c:
|
||||
The author of this software is David M. Gay.
|
||||
|
||||
Copyright (c) 1991, 1996 by Lucent Technologies.
|
||||
|
||||
Permission to use, copy, modify, and distribute this software for any
|
||||
purpose without fee is hereby granted, provided that this entire notice
|
||||
is included in all copies of any software which is or includes a copy
|
||||
or modification of this software and in all copies of the supporting
|
||||
documentation for such software.
|
||||
|
||||
THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
|
||||
WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
|
||||
REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
|
||||
OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
|
||||
|
||||
@@ -1,157 +0,0 @@
|
||||
module JQ where
|
||||
import Text.JSON
|
||||
import Text.JSON.String
|
||||
import Data.Maybe
|
||||
import Data.List (sortBy,sort,groupBy)
|
||||
import Data.Function (on)
|
||||
import Data.Ord (comparing)
|
||||
import Control.Monad
|
||||
import Control.Monad.Writer
|
||||
import Control.Monad.List
|
||||
import Control.Monad.Reader
|
||||
|
||||
type Path = [Either Int String]
|
||||
|
||||
type Program = JSValue -> [(JSValue, Path)]
|
||||
|
||||
type JQ = ReaderT JSValue (WriterT Path [])
|
||||
|
||||
runJQ :: JQ a -> JSValue -> [a]
|
||||
runJQ prog val = map fst $ runWriterT $ runReaderT prog val
|
||||
|
||||
(>|) :: JQ JSValue -> JQ a -> JQ a
|
||||
a >| b = do
|
||||
val <- a
|
||||
local (const val) b
|
||||
|
||||
collect :: JQ a -> JQ [a]
|
||||
collect prog = do
|
||||
arg <- ask
|
||||
return $ runJQ prog arg
|
||||
|
||||
collectPaths :: JQ a -> JQ [(a,Path)]
|
||||
collectPaths prog = do
|
||||
arg <- ask
|
||||
return $ runWriterT $ runReaderT prog arg
|
||||
|
||||
insert :: JSValue -> (JSValue, Path) -> JSValue
|
||||
insert base (replace, []) = replace
|
||||
insert (JSArray values) (replace, ((Left n):rest)) = JSArray values'
|
||||
where
|
||||
(left, (_:right)) = splitAt n values
|
||||
values' = left ++ [replace] ++ right
|
||||
insert (JSObject obj) (replace, ((Right k):rest))= JSObject $ toJSObject obj'
|
||||
where
|
||||
withoutK = filter ((/= k) . fst) $ fromJSObject obj
|
||||
obj' = (k, replace):withoutK
|
||||
|
||||
|
||||
eqj a b = JSBool $ a == b
|
||||
|
||||
|
||||
liftp :: (JSValue -> JSValue) -> JQ JSValue
|
||||
liftp f = liftM f ask
|
||||
|
||||
idp = undefined
|
||||
failp t = []
|
||||
|
||||
constp :: JSValue -> Program
|
||||
constp t t' = idp t
|
||||
|
||||
anyj :: [JSValue] -> Bool
|
||||
anyj values = any isTrue values
|
||||
where
|
||||
isTrue (JSBool False) = False
|
||||
isTrue (JSNull) = False
|
||||
isTrue _ = True
|
||||
|
||||
selectp prog = do
|
||||
match <- collect prog
|
||||
guard $ anyj match
|
||||
ask
|
||||
|
||||
constStr :: String -> JQ JSValue
|
||||
constStr = return . JSString . toJSString
|
||||
|
||||
constInt :: Int -> JQ JSValue
|
||||
constInt = return . JSRational False . toRational
|
||||
|
||||
updatep p = do
|
||||
t <- ask
|
||||
liftM (foldl insert t) $ collectPaths p
|
||||
|
||||
arrayp prog = liftM JSArray $ collect prog
|
||||
|
||||
|
||||
childp' :: JSValue -> JQ JSValue
|
||||
childp' (JSArray values) = msum [tell [Left i] >> return v | (v,i) <- zip values [0..]]
|
||||
childp' (JSObject obj) = msum [tell [Right k] >> return v | (k,v) <- fromJSObject obj]
|
||||
childp' _ = mzero
|
||||
|
||||
childp = ask >>= childp'
|
||||
|
||||
--findp :: Program -> Program
|
||||
findp prog = do
|
||||
found <- collect prog
|
||||
if anyj found then ask else childp >| findp prog
|
||||
|
||||
groupp prog = do
|
||||
list <- ask
|
||||
case list of
|
||||
JSArray values -> do
|
||||
marked <- forM values $ \v -> do
|
||||
m <- collect (return v >| prog)
|
||||
return (m,v)
|
||||
msum $
|
||||
map (return . JSArray . map snd) $
|
||||
groupBy ((==) `on` fst) $
|
||||
sortBy (comparing fst) $
|
||||
marked
|
||||
_ -> return JSNull
|
||||
|
||||
|
||||
|
||||
|
||||
withArray f (JSArray values) = JSArray $ f values
|
||||
withArray f x = x
|
||||
|
||||
callp "select" [p] = selectp p
|
||||
callp "find" [p] = findp p
|
||||
callp "set" [p] = updatep p
|
||||
callp "sort" [] = liftp (withArray sort)
|
||||
callp "group" [p] = groupp p
|
||||
|
||||
lookupj :: JSValue -> JSValue -> JQ JSValue
|
||||
lookupj (JSArray values) (JSRational _ n) = do
|
||||
let idx = round n
|
||||
tell [Left idx]
|
||||
if idx >= 0 && idx < length values
|
||||
then return $ values !! idx
|
||||
else return $ JSNull
|
||||
lookupj (JSObject obj) (JSString s) = do
|
||||
tell [Right (fromJSString s)]
|
||||
case (lookup (fromJSString s) (fromJSObject obj)) of
|
||||
Just x -> return x
|
||||
Nothing -> return JSNull
|
||||
lookupj _ _ = mzero
|
||||
|
||||
|
||||
plusj (JSRational _ n1) (JSRational _ n2) = JSRational True (n1 + n2)
|
||||
plusj (JSString s1) (JSString s2) = JSString $ toJSString (fromJSString s1 ++ fromJSString s2)
|
||||
plusj (JSArray a1) (JSArray a2) = JSArray $ a1 ++ a2
|
||||
|
||||
|
||||
js :: JSON a => a -> JSValue
|
||||
js = showJSON
|
||||
|
||||
index s = do
|
||||
v <- ask
|
||||
lookupj v (js s)
|
||||
|
||||
|
||||
dictp progs = do
|
||||
liftM (JSObject . toJSObject) $ forM progs $ \(k,v) -> do
|
||||
JSString k' <- k
|
||||
v' <- v
|
||||
return (fromJSString k', v')
|
||||
|
||||
@@ -1,101 +0,0 @@
|
||||
{
|
||||
module Lexer where
|
||||
import Control.Monad.Error
|
||||
}
|
||||
|
||||
%wrapper "monadUserState"
|
||||
|
||||
$digit = 0-9
|
||||
$alpha = [a-zA-Z_]
|
||||
@reserved = "."|"["|"]"|","|":"|"("|")"|"{"|"}"|"|"|"=="|"+"
|
||||
@ident = $alpha [$alpha $digit]*
|
||||
@string = \" ($printable)* \"
|
||||
|
||||
|
||||
tokens :-
|
||||
|
||||
<0> $white+ ;
|
||||
<0> @reserved { tok TRes }
|
||||
<0> @ident { tok TIdent }
|
||||
<0> $digit+ { tok $ TInt . read }
|
||||
|
||||
|
||||
<0> \" { enterString }
|
||||
<string> \" { leaveString }
|
||||
<string> ($printable # [\"\\]) { pushString id }
|
||||
<string> \\ [\"\\\/] { pushString (drop 1) }
|
||||
<string> \\ [nrt] { pushString (escape . drop 1) }
|
||||
--<string> \\ 'u' [0-9a-fA-F]{4}
|
||||
-- { pushString (parseUnicode . drop 2) }
|
||||
|
||||
-- @string { \s -> TString $ init $ tail s}
|
||||
|
||||
|
||||
{
|
||||
|
||||
escape :: String -> String
|
||||
escape "r" = "\r"
|
||||
escape "n" = "\n"
|
||||
escape "t" = "\t"
|
||||
|
||||
getState :: Alex AlexState
|
||||
getState = Alex $ \s -> Right (s, s)
|
||||
|
||||
getUserState :: Alex AlexUserState
|
||||
getUserState = liftM alex_ust getState
|
||||
|
||||
setUserState :: AlexUserState -> Alex ()
|
||||
setUserState s' = Alex $ \s -> Right (s{alex_ust = s'}, ())
|
||||
|
||||
alexEOF = return $ Nothing
|
||||
|
||||
enterString input len = do
|
||||
setUserState []
|
||||
alexSetStartCode string
|
||||
skip input len
|
||||
|
||||
pushString f i@(p, _, s) len = do
|
||||
buf <- getUserState
|
||||
setUserState (buf ++ [f $ take len s])
|
||||
skip i len
|
||||
|
||||
leaveString input len = do
|
||||
s <- getUserState
|
||||
alexSetStartCode 0
|
||||
return $ Just $ TString $ concat s
|
||||
|
||||
|
||||
tok f (p,_,s) len = return $ Just $ f (take len s)
|
||||
data Token = TRes String | TString String | TIdent String | TInt Int
|
||||
|
||||
instance Show Token where
|
||||
show (TRes t) = "token " ++ t
|
||||
show (TString t) = "string " ++ t
|
||||
show (TIdent t) = "identifier " ++ t
|
||||
show (TInt t) = "integer " ++ show t
|
||||
|
||||
|
||||
type AlexUserState = [String]
|
||||
|
||||
alexInitUserState = undefined
|
||||
|
||||
wrapError (Alex scanner) = Alex $ \s -> case scanner s of
|
||||
Left message -> Left (message ++ " at " ++ showpos (alex_pos s))
|
||||
where
|
||||
showpos (AlexPn off line col) = "line " ++ show line ++ ", column " ++ show col
|
||||
x -> x
|
||||
|
||||
scanner = do
|
||||
tok <- wrapError alexMonadScan
|
||||
case tok of
|
||||
Nothing -> do
|
||||
s <- getState
|
||||
case alex_scd s of
|
||||
0 -> return []
|
||||
string -> alexError "Unterminated string literal"
|
||||
Just tok -> liftM (tok:) scanner
|
||||
|
||||
runLexer :: String -> Either String [Token]
|
||||
runLexer input = runAlex input scanner
|
||||
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
import Parser
|
||||
import Lexer
|
||||
import JQ
|
||||
import Text.JSON
|
||||
import Text.JSON.String
|
||||
import System.Environment
|
||||
import Control.Monad
|
||||
import System.IO
|
||||
|
||||
|
||||
parseJS :: String -> JSValue
|
||||
parseJS s = case runGetJSON readJSValue s of
|
||||
Left err -> error err
|
||||
Right val -> val
|
||||
|
||||
|
||||
main = do
|
||||
[program] <- getArgs
|
||||
json <- liftM parseJS $ hGetContents stdin
|
||||
case runLexer program >>= runParser of
|
||||
Left err -> putStrLn err
|
||||
Right program -> mapM_ (putStrLn . encode) (runJQ program json)
|
||||
@@ -0,0 +1,61 @@
|
||||
CC=gcc -Wextra -Wall -Wno-missing-field-initializers -Wno-unused-parameter -std=gnu99 -ggdb -Wno-unused-function
|
||||
prefix=/usr/local
|
||||
|
||||
.PHONY: all clean releasedep tarball install uninstall test releasetag
|
||||
all: jq
|
||||
|
||||
clean:
|
||||
make -Bnd | grep 'Must remake target' | \
|
||||
sed 's/.*`\(.*\)'\''.*/\1/' | grep -v '^all$$' | \
|
||||
xargs rm
|
||||
|
||||
|
||||
lexer.gen.c: lexer.l
|
||||
flex -o lexer.gen.c --header-file=lexer.gen.h lexer.l
|
||||
lexer.gen.h: lexer.gen.c
|
||||
|
||||
parser.gen.c: parser.y lexer.gen.h
|
||||
bison -W -d parser.y -v --report-file=parser.gen.info -o $@
|
||||
parser.gen.h: parser.gen.c
|
||||
|
||||
jv_utf8_tables.gen.h: gen_utf8_tables.py
|
||||
python $^ > $@
|
||||
jv_unicode.c: jv_utf8_tables.gen.h
|
||||
|
||||
version.gen.h: VERSION
|
||||
sed 's/.*/#define JQ_VERSION "&"/' $^ > $@
|
||||
main.c: version.gen.h
|
||||
|
||||
JQ_SRC=parser.gen.c lexer.gen.c opcode.c bytecode.c compile.c execute.c builtin.c jv.c jv_parse.c jv_print.c jv_dtoa.c jv_unicode.c
|
||||
|
||||
|
||||
jq_test: $(JQ_SRC) jq_test.c
|
||||
$(CC) -DJQ_DEBUG=1 -o $@ $^
|
||||
|
||||
jq: $(JQ_SRC) main.c
|
||||
$(CC) -O -DJQ_DEBUG=0 -o $@ $^
|
||||
|
||||
test: jq_test
|
||||
valgrind --error-exitcode=1 -q --leak-check=full ./jq_test >/dev/null
|
||||
|
||||
|
||||
releasedep: lexer.gen.c parser.gen.c jv_utf8_tables.gen.h
|
||||
|
||||
releasetag:
|
||||
git tag -s "jq-$$(cat VERSION)" -m "jq release $$(cat VERSION)"
|
||||
|
||||
docs/content/2.download/source/jq.tgz: jq
|
||||
mkdir -p `dirname $@`
|
||||
tar -czvf $@ `git ls-files; ls *.gen.*`
|
||||
|
||||
tarball: docs/content/2.download/source/jq.tgz
|
||||
|
||||
install: jq
|
||||
install -d -m 0755 $(prefix)/bin
|
||||
install -m 0755 jq $(prefix)/bin
|
||||
|
||||
uninstall:
|
||||
test -d $(prefix)/bin && \
|
||||
cd $(prefix)/bin && \
|
||||
rm -f jq
|
||||
|
||||
@@ -1,78 +0,0 @@
|
||||
{
|
||||
module Parser where
|
||||
import Lexer
|
||||
import JQ
|
||||
import Text.JSON
|
||||
import Debug.Trace
|
||||
import Data.List
|
||||
import Control.Monad.Error
|
||||
import Control.Monad.Reader
|
||||
}
|
||||
|
||||
%name runParser Exp
|
||||
%tokentype { Token }
|
||||
|
||||
%monad { Either String }
|
||||
%error { \t -> fail $ "parse error: unexpected " ++ if null t then [] else (show $ head t) }
|
||||
|
||||
%token
|
||||
'|' { TRes "|" }
|
||||
'.' { TRes "." }
|
||||
'[' { TRes "[" }
|
||||
']' { TRes "]" }
|
||||
'{' { TRes "{" }
|
||||
'}' { TRes "}" }
|
||||
'(' { TRes "(" }
|
||||
')' { TRes ")" }
|
||||
',' { TRes "," }
|
||||
':' { TRes ":" }
|
||||
'==' { TRes "==" }
|
||||
'+' { TRes "+" }
|
||||
Ident { TIdent $$ }
|
||||
String { TString $$ }
|
||||
Int { TInt $$ }
|
||||
|
||||
%left '|'
|
||||
%left ','
|
||||
%nonassoc '=='
|
||||
%left '+'
|
||||
|
||||
%%
|
||||
|
||||
Exp
|
||||
: Exp '|' Exp { $1 >| $3 }
|
||||
| Exp ',' Exp { $1 `mplus` $3 }
|
||||
| Exp '==' Exp { liftM2 eqj $1 $3 }
|
||||
| Exp '+' Exp { liftM2 plusj $1 $3 }
|
||||
| Term { $1 }
|
||||
|
||||
ExpD
|
||||
: ExpD '|' ExpD { $1 >| $3 }
|
||||
| ExpD '==' ExpD { liftM2 eqj $1 $3 }
|
||||
| Term { $1 }
|
||||
|
||||
|
||||
Term
|
||||
: '.' { ask }
|
||||
| Term '.' Ident { $1 >| index $3 }
|
||||
| '.' Ident { index $2 }
|
||||
| String { constStr $1 }
|
||||
| Term '[' Exp ']' { do {t <- $1; i <- $3; lookupj t i} }
|
||||
| Term '[' ']' { $1 >| childp }
|
||||
| '(' Exp ')' { $2 }
|
||||
| '[' Exp ']' { arrayp $2 }
|
||||
| Int { constInt $1 }
|
||||
| '{' MkDict '}' { dictp $2 }
|
||||
| Ident '(' Exp ')' { callp $1 [$3] }
|
||||
| Ident { callp $1 [] }
|
||||
|
||||
MkDict
|
||||
: { [] }
|
||||
| MkDictPair { [$1] }
|
||||
| MkDictPair ',' MkDict { $1:$3 }
|
||||
|
||||
MkDictPair
|
||||
: Ident ':' ExpD { (constStr $1, $3) }
|
||||
| Ident { (constStr $1, index $1) }
|
||||
| String ':' ExpD { (constStr $1, $3) }
|
||||
| '(' Exp ')' ':' ExpD{ ($2, $5) }
|
||||
@@ -0,0 +1,14 @@
|
||||
jq
|
||||
==
|
||||
|
||||
jq is a command-line JSON processor.
|
||||
|
||||
If you want to learn to use jq, read the documentation at
|
||||
[http://stedolan.github.com/jq](http://stedolan.github.com/jq). This
|
||||
documentation is generated from the docs/ folder of this repository.
|
||||
|
||||
If you want to hack on jq, feel free, but be warned that its internals
|
||||
are not well-documented at the moment. Bring a hard hat and a shovel.
|
||||
|
||||
You can find some basic build instructions at
|
||||
[http://stedolan.github.com/jq/download](http://stedolan.github.com/jq/download)
|
||||
@@ -0,0 +1,323 @@
|
||||
#include <string.h>
|
||||
#include "builtin.h"
|
||||
#include "compile.h"
|
||||
#include "parser.h"
|
||||
#include "locfile.h"
|
||||
|
||||
enum {
|
||||
CMP_OP_LESS,
|
||||
CMP_OP_GREATER,
|
||||
CMP_OP_LESSEQ,
|
||||
CMP_OP_GREATEREQ
|
||||
} _cmp_op;
|
||||
|
||||
static void f_plus(jv input[], jv output[]) {
|
||||
jv_free(input[0]);
|
||||
jv a = input[2];
|
||||
jv b = input[1];
|
||||
if (jv_get_kind(a) == JV_KIND_NUMBER && jv_get_kind(b) == JV_KIND_NUMBER) {
|
||||
output[0] = jv_number(jv_number_value(a) +
|
||||
jv_number_value(b));
|
||||
} else if (jv_get_kind(a) == JV_KIND_STRING && jv_get_kind(b) == JV_KIND_STRING) {
|
||||
output[0] = jv_string_concat(a, b);
|
||||
} else if (jv_get_kind(a) == JV_KIND_ARRAY && jv_get_kind(b) == JV_KIND_ARRAY) {
|
||||
output[0] = jv_array_concat(a, b);
|
||||
} else if (jv_get_kind(a) == JV_KIND_OBJECT && jv_get_kind(b) == JV_KIND_OBJECT) {
|
||||
output[0] = jv_object_merge(a, b);
|
||||
} else {
|
||||
output[0] = jv_invalid_with_msg(jv_string_fmt("Attempted to add %s and %s",
|
||||
jv_kind_name(jv_get_kind(a)),
|
||||
jv_kind_name(jv_get_kind(b))));
|
||||
jv_free(a);
|
||||
jv_free(b);
|
||||
}
|
||||
}
|
||||
|
||||
static void f_minus(jv input[], jv output[]) {
|
||||
jv_free(input[0]);
|
||||
jv a = input[2];
|
||||
jv b = input[1];
|
||||
if (jv_get_kind(a) == JV_KIND_NUMBER && jv_get_kind(b) == JV_KIND_NUMBER) {
|
||||
output[0] = jv_number(jv_number_value(a) - jv_number_value(b));
|
||||
} else if (jv_get_kind(a) == JV_KIND_ARRAY && jv_get_kind(b) == JV_KIND_ARRAY) {
|
||||
jv out = jv_array();
|
||||
for (int i=0; i<jv_array_length(jv_copy(a)); i++) {
|
||||
jv x = jv_array_get(jv_copy(a), i);
|
||||
int include = 1;
|
||||
for (int j=0; j<jv_array_length(jv_copy(b)); j++) {
|
||||
if (jv_equal(jv_copy(x), jv_array_get(jv_copy(b), j))) {
|
||||
include = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (include)
|
||||
out = jv_array_append(out, jv_copy(x));
|
||||
jv_free(x);
|
||||
}
|
||||
jv_free(a);
|
||||
jv_free(b);
|
||||
output[0] = out;
|
||||
} else {
|
||||
output[0] = jv_invalid_with_msg(jv_string_fmt("Attempted to subtract %s and %s",
|
||||
jv_kind_name(jv_get_kind(a)),
|
||||
jv_kind_name(jv_get_kind(b))));
|
||||
jv_free(a);
|
||||
jv_free(b);
|
||||
}
|
||||
}
|
||||
|
||||
static void f_multiply(jv input[], jv output[]) {
|
||||
jv_free(input[0]);
|
||||
jv a = input[2];
|
||||
jv b = input[1];
|
||||
if (jv_get_kind(a) == JV_KIND_NUMBER && jv_get_kind(b) == JV_KIND_NUMBER) {
|
||||
output[0] = jv_number(jv_number_value(a) * jv_number_value(b));
|
||||
} else {
|
||||
output[0] = jv_invalid_with_msg(jv_string_fmt("Attempted to multiply %s and %s",
|
||||
jv_kind_name(jv_get_kind(a)),
|
||||
jv_kind_name(jv_get_kind(b))));
|
||||
jv_free(a);
|
||||
jv_free(b);
|
||||
}
|
||||
}
|
||||
|
||||
static void f_divide(jv input[], jv output[]) {
|
||||
jv_free(input[0]);
|
||||
jv a = input[2];
|
||||
jv b = input[1];
|
||||
if (jv_get_kind(a) == JV_KIND_NUMBER && jv_get_kind(b) == JV_KIND_NUMBER) {
|
||||
output[0] = jv_number(jv_number_value(a) / jv_number_value(b));
|
||||
} else {
|
||||
output[0] = jv_invalid_with_msg(jv_string_fmt("Attempted to divide %s by %s",
|
||||
jv_kind_name(jv_get_kind(a)),
|
||||
jv_kind_name(jv_get_kind(b))));
|
||||
jv_free(a);
|
||||
jv_free(b);
|
||||
}
|
||||
}
|
||||
|
||||
static void f_add(jv input[], jv output[]) {
|
||||
jv array = input[0];
|
||||
if (jv_get_kind(array) != JV_KIND_ARRAY) {
|
||||
output[0] = jv_invalid_with_msg(jv_string_fmt("Cannot add elements of an %s",
|
||||
jv_kind_name(jv_get_kind(array))));
|
||||
} else if (jv_array_length(jv_copy(array)) == 0) {
|
||||
output[0] = jv_null();
|
||||
} else {
|
||||
jv sum = jv_array_get(jv_copy(array), 0);
|
||||
for (int i = 1; i < jv_array_length(jv_copy(array)); i++) {
|
||||
jv x = jv_array_get(jv_copy(array), i);
|
||||
jv add_args[] = {jv_null(), x, sum};
|
||||
f_plus(add_args, &sum);
|
||||
}
|
||||
output[0] = sum;
|
||||
}
|
||||
jv_free(array);
|
||||
}
|
||||
|
||||
static void f_equal(jv input[], jv output[]) {
|
||||
jv_free(input[0]);
|
||||
output[0] = jv_bool(jv_equal(input[2], input[1]));
|
||||
}
|
||||
|
||||
static void order_cmp(jv input[], jv output[], int op) {
|
||||
jv_free(input[0]);
|
||||
jv a = input[2];
|
||||
jv b = input[1];
|
||||
if (jv_get_kind(a) == JV_KIND_NUMBER && jv_get_kind(b) == JV_KIND_NUMBER) {
|
||||
double da = jv_number_value(a);
|
||||
double db = jv_number_value(b);
|
||||
output[0] = jv_bool((op == CMP_OP_LESS && da < db) ||
|
||||
(op == CMP_OP_LESSEQ && da <= db) ||
|
||||
(op == CMP_OP_GREATEREQ && da >= db) ||
|
||||
(op == CMP_OP_GREATER && da > db));
|
||||
} else {
|
||||
output[0] = jv_invalid_with_msg(jv_string_fmt("Attempted to compare order of %s wrt %s",
|
||||
jv_kind_name(jv_get_kind(a)),
|
||||
jv_kind_name(jv_get_kind(b))));
|
||||
jv_free(a);
|
||||
jv_free(b);
|
||||
}
|
||||
}
|
||||
|
||||
static void f_less(jv input[], jv output[]) {
|
||||
order_cmp(input, output, CMP_OP_LESS);
|
||||
}
|
||||
|
||||
static void f_greater(jv input[], jv output[]) {
|
||||
order_cmp(input, output, CMP_OP_GREATER);
|
||||
}
|
||||
|
||||
static void f_lesseq(jv input[], jv output[]) {
|
||||
order_cmp(input, output, CMP_OP_LESSEQ);
|
||||
}
|
||||
|
||||
static void f_greatereq(jv input[], jv output[]) {
|
||||
order_cmp(input, output, CMP_OP_GREATEREQ);
|
||||
}
|
||||
|
||||
static void f_tonumber(jv input[], jv output[]) {
|
||||
if (jv_get_kind(input[0]) == JV_KIND_NUMBER) {
|
||||
output[0] = input[0];
|
||||
} else if (jv_get_kind(input[0]) == JV_KIND_STRING) {
|
||||
jv parsed = jv_parse(jv_string_value(input[0]));
|
||||
if (!jv_is_valid(parsed)) {
|
||||
jv_free(input[0]);
|
||||
output[0] = parsed;
|
||||
} else if (jv_get_kind(parsed) != JV_KIND_NUMBER) {
|
||||
output[0] = jv_invalid_with_msg(jv_string_fmt("'%s' is not a number",
|
||||
jv_string_value(input[0])));
|
||||
jv_free(input[0]);
|
||||
} else {
|
||||
jv_free(input[0]);
|
||||
output[0] = parsed;
|
||||
}
|
||||
} else {
|
||||
output[0] = jv_invalid_with_msg(jv_string_fmt("Cannot parse %s as a number",
|
||||
jv_kind_name(jv_get_kind(input[0]))));
|
||||
jv_free(input[0]);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static void f_length(jv input[], jv output[]) {
|
||||
if (jv_get_kind(input[0]) == JV_KIND_ARRAY) {
|
||||
output[0] = jv_number(jv_array_length(input[0]));
|
||||
} else if (jv_get_kind(input[0]) == JV_KIND_OBJECT) {
|
||||
output[0] = jv_number(jv_object_length(input[0]));
|
||||
} else if (jv_get_kind(input[0]) == JV_KIND_STRING) {
|
||||
output[0] = jv_number(jv_string_length(input[0]));
|
||||
} else {
|
||||
output[0] = jv_invalid_with_msg(jv_string_fmt("Cannot get the length of %s",
|
||||
jv_kind_name(jv_get_kind(input[0]))));
|
||||
jv_free(input[0]);
|
||||
}
|
||||
}
|
||||
|
||||
static void f_tostring(jv input[], jv output[]) {
|
||||
if (jv_get_kind(input[0]) == JV_KIND_STRING) {
|
||||
output[0] = input[0];
|
||||
} else {
|
||||
output[0] = jv_dump_string(input[0], 0);
|
||||
}
|
||||
}
|
||||
|
||||
static int string_cmp(const void* pa, const void* pb){
|
||||
const jv* a = pa;
|
||||
const jv* b = pb;
|
||||
int lena = jv_string_length(jv_copy(*a));
|
||||
int lenb = jv_string_length(jv_copy(*b));
|
||||
int minlen = lena < lenb ? lena : lenb;
|
||||
int r = memcmp(jv_string_value(*a), jv_string_value(*b), minlen);
|
||||
if (r == 0) r = lena - lenb;
|
||||
return r;
|
||||
}
|
||||
|
||||
static void f_keys(jv input[], jv output[]) {
|
||||
if (jv_get_kind(input[0]) == JV_KIND_OBJECT) {
|
||||
int nkeys = jv_object_length(jv_copy(input[0]));
|
||||
jv* keys = malloc(sizeof(jv) * nkeys);
|
||||
int kidx = 0;
|
||||
jv_object_foreach(i, input[0]) {
|
||||
keys[kidx++] = jv_object_iter_key(input[0], i);
|
||||
}
|
||||
qsort(keys, nkeys, sizeof(jv), string_cmp);
|
||||
output[0] = jv_array_sized(nkeys);
|
||||
for (int i = 0; i<nkeys; i++) {
|
||||
output[0] = jv_array_append(output[0], keys[i]);
|
||||
}
|
||||
free(keys);
|
||||
jv_free(input[0]);
|
||||
} else {
|
||||
output[0] = jv_invalid_with_msg(jv_string_fmt("'keys' only supports object, not %s",
|
||||
jv_kind_name(jv_get_kind(input[0]))));
|
||||
jv_free(input[0]);
|
||||
}
|
||||
}
|
||||
|
||||
static void f_type(jv input[], jv output[]) {
|
||||
output[0] = jv_string(jv_kind_name(jv_get_kind(input[0])));
|
||||
jv_free(input[0]);
|
||||
}
|
||||
|
||||
static block j_empty() {
|
||||
return gen_op_block_defn(CLOSURE_CREATE, "empty", gen_op_simple(BACKTRACK));
|
||||
}
|
||||
|
||||
static block j_false() {
|
||||
return gen_op_block_defn(CLOSURE_CREATE, "false",
|
||||
block_join(gen_op_const(LOADK, jv_false()),
|
||||
gen_op_simple(RET)));
|
||||
}
|
||||
|
||||
static block j_true() {
|
||||
return gen_op_block_defn(CLOSURE_CREATE, "true",
|
||||
block_join(gen_op_const(LOADK, jv_true()),
|
||||
gen_op_simple(RET)));
|
||||
}
|
||||
|
||||
static block j_null() {
|
||||
return gen_op_block_defn(CLOSURE_CREATE, "null",
|
||||
block_join(gen_op_const(LOADK, jv_null()),
|
||||
gen_op_simple(RET)));
|
||||
}
|
||||
|
||||
static block j_not() {
|
||||
return gen_op_block_defn(CLOSURE_CREATE, "not",
|
||||
block_join(gen_condbranch(gen_op_const(LOADK, jv_false()),
|
||||
gen_op_const(LOADK, jv_true())),
|
||||
gen_op_simple(RET)));
|
||||
}
|
||||
|
||||
static struct cfunction function_list[] = {
|
||||
{f_plus, "_plus", CALL_BUILTIN_3_1},
|
||||
{f_minus, "_minus", CALL_BUILTIN_3_1},
|
||||
{f_multiply, "_multiply", CALL_BUILTIN_3_1},
|
||||
{f_divide, "_divide", CALL_BUILTIN_3_1},
|
||||
{f_tonumber, "tonumber", CALL_BUILTIN_1_1},
|
||||
{f_tostring, "tostring", CALL_BUILTIN_1_1},
|
||||
{f_keys, "keys", CALL_BUILTIN_1_1},
|
||||
{f_equal, "_equal", CALL_BUILTIN_3_1},
|
||||
{f_less, "_less", CALL_BUILTIN_3_1},
|
||||
{f_greater, "_greater", CALL_BUILTIN_3_1},
|
||||
{f_lesseq, "_lesseq", CALL_BUILTIN_3_1},
|
||||
{f_greatereq, "_greatereq", CALL_BUILTIN_3_1},
|
||||
{f_length, "length", CALL_BUILTIN_1_1},
|
||||
{f_type, "type", CALL_BUILTIN_1_1},
|
||||
{f_add, "add", CALL_BUILTIN_1_1},
|
||||
};
|
||||
|
||||
static struct symbol_table cbuiltins = {function_list, sizeof(function_list)/sizeof(function_list[0])};
|
||||
|
||||
typedef block (*bytecoded_builtin)();
|
||||
static bytecoded_builtin bytecoded_builtins[] = {
|
||||
j_empty,
|
||||
j_false,
|
||||
j_true,
|
||||
j_null,
|
||||
j_not,
|
||||
};
|
||||
|
||||
static const char* jq_builtins[] = {
|
||||
"def map(f): [.[] | f];",
|
||||
"def select(f): if f then . else empty end;",
|
||||
};
|
||||
|
||||
|
||||
block builtins_bind(block b) {
|
||||
for (int i=(int)(sizeof(jq_builtins)/sizeof(jq_builtins[0]))-1; i>=0; i--) {
|
||||
struct locfile src;
|
||||
locfile_init(&src, jq_builtins[i], strlen(jq_builtins[i]));
|
||||
block funcs;
|
||||
int nerrors = jq_parse_library(&src, &funcs);
|
||||
assert(!nerrors);
|
||||
b = block_bind(funcs, b, OP_IS_CALL_PSEUDO);
|
||||
locfile_free(&src);
|
||||
}
|
||||
block builtins = gen_noop();
|
||||
for (unsigned i=0; i<sizeof(bytecoded_builtins)/sizeof(bytecoded_builtins[0]); i++) {
|
||||
block_append(&builtins, bytecoded_builtins[i]());
|
||||
}
|
||||
b = block_bind(builtins, b, OP_IS_CALL_PSEUDO);
|
||||
return gen_cbinding(&cbuiltins, b);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#ifndef BUILTIN_H
|
||||
#define BUILTIN_H
|
||||
|
||||
#include "bytecode.h"
|
||||
#include "compile.h"
|
||||
|
||||
block builtins_bind(block);
|
||||
|
||||
#endif
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "bytecode.h"
|
||||
#include "opcode.h"
|
||||
|
||||
static int bytecode_operation_length(uint16_t* codeptr) {
|
||||
if (opcode_describe(*codeptr)->flags & OP_HAS_VARIABLE_LENGTH_ARGLIST) {
|
||||
return 2 + codeptr[1] * 2;
|
||||
} else {
|
||||
return opcode_length(*codeptr);
|
||||
}
|
||||
}
|
||||
|
||||
void dump_disassembly(int indent, struct bytecode* bc) {
|
||||
if (bc->nclosures > 0) {
|
||||
printf("%*s[params: %d]\n", indent, "", bc->nclosures);
|
||||
}
|
||||
dump_code(indent, bc);
|
||||
for (int i=0; i<bc->nsubfunctions; i++) {
|
||||
printf("%*ssubfn[%d]:\n", indent, "", i);
|
||||
dump_disassembly(indent+2, bc->subfunctions[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void dump_code(int indent, struct bytecode* bc) {
|
||||
int pc = 0;
|
||||
while (pc < bc->codelen) {
|
||||
printf("%*s", indent, "");
|
||||
dump_operation(bc, bc->code + pc);
|
||||
printf("\n");
|
||||
pc += bytecode_operation_length(bc->code + pc);
|
||||
}
|
||||
}
|
||||
|
||||
void dump_operation(struct bytecode* bc, uint16_t* codeptr) {
|
||||
int pc = codeptr - bc->code;
|
||||
printf("%04d ", pc);
|
||||
const struct opcode_description* op = opcode_describe(bc->code[pc++]);
|
||||
printf("%s", op->name);
|
||||
if (op->length > 1) {
|
||||
uint16_t imm = bc->code[pc++];
|
||||
if (op->flags & OP_HAS_VARIABLE_LENGTH_ARGLIST) {
|
||||
for (int i=0; i<imm; i++) {
|
||||
uint16_t level = bc->code[pc++];
|
||||
uint16_t idx = bc->code[pc++];
|
||||
if (idx & ARG_NEWCLOSURE) {
|
||||
printf(" subfn[%d]", idx & ~ARG_NEWCLOSURE);
|
||||
} else {
|
||||
printf(" param[%d]", idx);
|
||||
}
|
||||
if (level) {
|
||||
printf("^%d", level);
|
||||
}
|
||||
}
|
||||
} else if (op->flags & OP_HAS_BRANCH) {
|
||||
printf(" %04d", pc + imm);
|
||||
} else if (op->flags & OP_HAS_CONSTANT) {
|
||||
printf(" ");
|
||||
jv_dump(jv_array_get(jv_copy(bc->constants), imm), 0);
|
||||
} else if (op->flags & OP_HAS_VARIABLE) {
|
||||
uint16_t v = bc->code[pc++];
|
||||
printf(" v%d", v);
|
||||
if (imm) {
|
||||
printf("^%d", imm);
|
||||
}
|
||||
} else {
|
||||
printf(" %d", imm);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void symbol_table_free(struct symbol_table* syms) {
|
||||
free(syms->cfunctions);
|
||||
free(syms);
|
||||
}
|
||||
|
||||
void bytecode_free(struct bytecode* bc) {
|
||||
free(bc->code);
|
||||
jv_free(bc->constants);
|
||||
for (int i=0; i<bc->nsubfunctions; i++)
|
||||
bytecode_free(bc->subfunctions[i]);
|
||||
if (!bc->parent)
|
||||
symbol_table_free(bc->globals);
|
||||
free(bc->subfunctions);
|
||||
free(bc);
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
#ifndef BYTECODE_H
|
||||
#define BYTECODE_H
|
||||
#include <stdint.h>
|
||||
|
||||
#include "jv.h"
|
||||
#include "opcode.h"
|
||||
|
||||
typedef void (*cfunction_ptr)(jv input[], jv output[]);
|
||||
|
||||
struct cfunction {
|
||||
cfunction_ptr fptr;
|
||||
const char* name;
|
||||
opcode callop;
|
||||
};
|
||||
|
||||
#define MAX_CFUNCTION_ARGS 10
|
||||
struct symbol_table {
|
||||
struct cfunction* cfunctions;
|
||||
int ncfunctions;
|
||||
};
|
||||
|
||||
// The bytecode format matters in:
|
||||
// execute.c - interpreter
|
||||
// compile.c - compiler
|
||||
// bytecode.c - disassembler
|
||||
|
||||
#define ARG_NEWCLOSURE 0x1000
|
||||
|
||||
struct bytecode {
|
||||
uint16_t* code;
|
||||
int codelen;
|
||||
|
||||
int nlocals;
|
||||
int nclosures;
|
||||
|
||||
jv constants; // JSON array of constants
|
||||
struct symbol_table* globals;
|
||||
|
||||
struct bytecode** subfunctions;
|
||||
int nsubfunctions;
|
||||
|
||||
struct bytecode* parent;
|
||||
};
|
||||
|
||||
void dump_disassembly(int, struct bytecode* code);
|
||||
void dump_code(int, struct bytecode* code);
|
||||
void dump_operation(struct bytecode* bc, uint16_t* op);
|
||||
|
||||
void symbol_table_free(struct symbol_table* syms);
|
||||
void bytecode_free(struct bytecode* bc);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,613 @@
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "opcode.h"
|
||||
#include "compile.h"
|
||||
#include "locfile.h"
|
||||
|
||||
struct inst {
|
||||
struct inst* next;
|
||||
struct inst* prev;
|
||||
|
||||
opcode op;
|
||||
|
||||
union {
|
||||
uint16_t intval;
|
||||
struct inst* target;
|
||||
jv constant;
|
||||
struct cfunction* cfunc;
|
||||
} imm;
|
||||
|
||||
location source;
|
||||
|
||||
// Binding
|
||||
// An instruction requiring binding (for parameters/variables)
|
||||
// is in one of three states:
|
||||
// bound_by = NULL - Unbound free variable
|
||||
// bound_by = self - This instruction binds a variable
|
||||
// bound_by = other - Uses variable bound by other instruction
|
||||
// The immediate field is generally not meaningful until instructions
|
||||
// are bound, and even then only for instructions which bind.
|
||||
struct inst* bound_by;
|
||||
char* symbol;
|
||||
block subfn;
|
||||
|
||||
// This instruction is compiled as part of which function?
|
||||
// (only used during block_compile)
|
||||
struct bytecode* compiled;
|
||||
|
||||
int bytecode_pos; // position just after this insn
|
||||
};
|
||||
|
||||
static inst* inst_new(opcode op) {
|
||||
inst* i = malloc(sizeof(inst));
|
||||
i->next = i->prev = 0;
|
||||
i->op = op;
|
||||
i->bytecode_pos = -1;
|
||||
i->bound_by = 0;
|
||||
i->symbol = 0;
|
||||
i->subfn = gen_noop();
|
||||
i->source = UNKNOWN_LOCATION;
|
||||
return i;
|
||||
}
|
||||
|
||||
static void inst_free(struct inst* i) {
|
||||
free(i->symbol);
|
||||
if (opcode_describe(i->op)->flags & OP_HAS_BLOCK) {
|
||||
block_free(i->subfn);
|
||||
}
|
||||
if (opcode_describe(i->op)->flags & OP_HAS_CONSTANT) {
|
||||
jv_free(i->imm.constant);
|
||||
}
|
||||
free(i);
|
||||
}
|
||||
|
||||
static block inst_block(inst* i) {
|
||||
block b = {i,i};
|
||||
return b;
|
||||
}
|
||||
|
||||
static int block_is_single(block b) {
|
||||
return b.first && b.first == b.last;
|
||||
}
|
||||
|
||||
static inst* block_take(block* b) {
|
||||
if (b->first == 0) return 0;
|
||||
inst* i = b->first;
|
||||
if (i->next) {
|
||||
i->next->prev = 0;
|
||||
b->first = i->next;
|
||||
i->next = 0;
|
||||
} else {
|
||||
b->first = 0;
|
||||
b->last = 0;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
block gen_location(location loc, block b) {
|
||||
for (inst* i = b.first; i; i = i->next) {
|
||||
if (i->source.start == UNKNOWN_LOCATION.start &&
|
||||
i->source.end == UNKNOWN_LOCATION.end) {
|
||||
i->source = loc;
|
||||
}
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
block gen_noop() {
|
||||
block b = {0,0};
|
||||
return b;
|
||||
}
|
||||
|
||||
block gen_op_simple(opcode op) {
|
||||
assert(opcode_describe(op)->length == 1);
|
||||
return inst_block(inst_new(op));
|
||||
}
|
||||
|
||||
|
||||
block gen_op_const(opcode op, jv constant) {
|
||||
assert(opcode_describe(op)->flags & OP_HAS_CONSTANT);
|
||||
inst* i = inst_new(op);
|
||||
i->imm.constant = constant;
|
||||
return inst_block(i);
|
||||
}
|
||||
|
||||
block gen_op_target(opcode op, block target) {
|
||||
assert(opcode_describe(op)->flags & OP_HAS_BRANCH);
|
||||
assert(target.last);
|
||||
inst* i = inst_new(op);
|
||||
i->imm.target = target.last;
|
||||
return inst_block(i);
|
||||
}
|
||||
|
||||
block gen_op_targetlater(opcode op) {
|
||||
assert(opcode_describe(op)->flags & OP_HAS_BRANCH);
|
||||
inst* i = inst_new(op);
|
||||
i->imm.target = 0;
|
||||
return inst_block(i);
|
||||
}
|
||||
void inst_set_target(block b, block target) {
|
||||
assert(block_is_single(b));
|
||||
assert(opcode_describe(b.first->op)->flags & OP_HAS_BRANCH);
|
||||
assert(target.last);
|
||||
b.first->imm.target = target.last;
|
||||
}
|
||||
|
||||
block gen_op_var_unbound(opcode op, const char* name) {
|
||||
assert(opcode_describe(op)->flags & OP_HAS_VARIABLE);
|
||||
inst* i = inst_new(op);
|
||||
i->symbol = strdup(name);
|
||||
return inst_block(i);
|
||||
}
|
||||
|
||||
block gen_op_var_bound(opcode op, block binder) {
|
||||
assert(block_is_single(binder));
|
||||
block b = gen_op_var_unbound(op, binder.first->symbol);
|
||||
b.first->bound_by = binder.first;
|
||||
return b;
|
||||
}
|
||||
|
||||
block gen_op_symbol(opcode op, const char* sym) {
|
||||
assert(opcode_describe(op)->flags & OP_HAS_SYMBOL);
|
||||
inst* i = inst_new(op);
|
||||
i->symbol = strdup(sym);
|
||||
return inst_block(i);
|
||||
}
|
||||
|
||||
block gen_op_block_defn(opcode op, const char* name, block block) {
|
||||
assert(opcode_describe(op)->flags & OP_IS_CALL_PSEUDO);
|
||||
assert(opcode_describe(op)->flags & OP_HAS_BLOCK);
|
||||
inst* i = inst_new(op);
|
||||
i->subfn = block;
|
||||
i->symbol = strdup(name);
|
||||
return inst_block(i);
|
||||
}
|
||||
|
||||
static void block_bind_subblock(block binder, block body, int bindflags);
|
||||
block gen_op_block_defn_rec(opcode op, const char* name, block blk) {
|
||||
block b = gen_op_block_defn(op, name, blk);
|
||||
block_bind_subblock(b, b, OP_IS_CALL_PSEUDO | OP_HAS_BINDING);
|
||||
return b;
|
||||
}
|
||||
|
||||
block gen_op_block_unbound(opcode op, const char* name) {
|
||||
assert(opcode_describe(op)->flags & OP_IS_CALL_PSEUDO);
|
||||
inst* i = inst_new(op);
|
||||
i->symbol = strdup(name);
|
||||
return inst_block(i);
|
||||
}
|
||||
|
||||
block gen_op_block_bound(opcode op, block binder) {
|
||||
assert(block_is_single(binder));
|
||||
block b = gen_op_block_unbound(op, binder.first->symbol);
|
||||
b.first->bound_by = binder.first;
|
||||
return b;
|
||||
}
|
||||
|
||||
block gen_op_call(opcode op, block arglist) {
|
||||
assert(opcode_describe(op)->flags & OP_HAS_VARIABLE_LENGTH_ARGLIST);
|
||||
inst* i = inst_new(op);
|
||||
block prelude = gen_noop();
|
||||
block call = inst_block(i);
|
||||
int nargs = 0;
|
||||
inst* curr = 0;
|
||||
while ((curr = block_take(&arglist))) {
|
||||
assert(opcode_describe(curr->op)->flags & OP_IS_CALL_PSEUDO);
|
||||
block bcurr = inst_block(curr);
|
||||
switch (curr->op) {
|
||||
default: assert(0 && "Unknown type of parameter"); break;
|
||||
case CLOSURE_REF:
|
||||
block_append(&call, bcurr);
|
||||
break;
|
||||
case CLOSURE_CREATE:
|
||||
block_append(&prelude, bcurr);
|
||||
block_append(&call, gen_op_block_bound(CLOSURE_REF, bcurr));
|
||||
break;
|
||||
}
|
||||
nargs++;
|
||||
}
|
||||
assert(nargs < 100); //FIXME
|
||||
i->imm.intval = nargs;
|
||||
return block_join(prelude, call);
|
||||
}
|
||||
|
||||
static void inst_join(inst* a, inst* b) {
|
||||
assert(a && b);
|
||||
assert(!a->next);
|
||||
assert(!b->prev);
|
||||
a->next = b;
|
||||
b->prev = a;
|
||||
}
|
||||
|
||||
void block_append(block* b, block b2) {
|
||||
if (b2.first) {
|
||||
if (b->last) {
|
||||
inst_join(b->last, b2.first);
|
||||
} else {
|
||||
b->first = b2.first;
|
||||
}
|
||||
b->last = b2.last;
|
||||
}
|
||||
}
|
||||
|
||||
block block_join(block a, block b) {
|
||||
block c = a;
|
||||
block_append(&c, b);
|
||||
return c;
|
||||
}
|
||||
|
||||
int block_has_only_binders(block binders, int bindflags) {
|
||||
bindflags |= OP_HAS_BINDING;
|
||||
for (inst* curr = binders.first; curr; curr = curr->next) {
|
||||
if ((opcode_describe(curr->op)->flags & bindflags) != bindflags) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void block_bind_subblock(block binder, block body, int bindflags) {
|
||||
assert(block_is_single(binder));
|
||||
assert((opcode_describe(binder.first->op)->flags & bindflags) == bindflags);
|
||||
assert(binder.first->symbol);
|
||||
assert(binder.first->bound_by == 0 || binder.first->bound_by == binder.first);
|
||||
|
||||
binder.first->bound_by = binder.first;
|
||||
for (inst* i = body.first; i; i = i->next) {
|
||||
int flags = opcode_describe(i->op)->flags;
|
||||
if ((flags & bindflags) == bindflags &&
|
||||
i->bound_by == 0 &&
|
||||
!strcmp(i->symbol, binder.first->symbol)) {
|
||||
// bind this instruction
|
||||
i->bound_by = binder.first;
|
||||
}
|
||||
if (flags & OP_HAS_BLOCK) {
|
||||
// binding recurses into closures
|
||||
block_bind_subblock(binder, i->subfn, bindflags);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
block block_bind(block binder, block body, int bindflags) {
|
||||
assert(block_has_only_binders(binder, bindflags));
|
||||
bindflags |= OP_HAS_BINDING;
|
||||
for (inst* curr = binder.first; curr; curr = curr->next) {
|
||||
block_bind_subblock(inst_block(curr), body, bindflags);
|
||||
}
|
||||
return block_join(binder, body);
|
||||
}
|
||||
|
||||
|
||||
block gen_subexp(block a) {
|
||||
block c = gen_noop();
|
||||
block_append(&c, gen_op_simple(DUP));
|
||||
block_append(&c, a);
|
||||
block_append(&c, gen_op_simple(SWAP));
|
||||
return c;
|
||||
}
|
||||
|
||||
block gen_both(block a, block b) {
|
||||
block c = gen_noop();
|
||||
block jump = gen_op_targetlater(JUMP);
|
||||
block fork = gen_op_targetlater(FORK);
|
||||
block_append(&c, fork);
|
||||
block_append(&c, a);
|
||||
block_append(&c, jump);
|
||||
inst_set_target(fork, c);
|
||||
block_append(&c, b);
|
||||
inst_set_target(jump, c);
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
block gen_collect(block expr) {
|
||||
block c = gen_noop();
|
||||
block_append(&c, gen_op_simple(DUP));
|
||||
block_append(&c, gen_op_const(LOADK, jv_array()));
|
||||
block array_var = block_bind(gen_op_var_unbound(STOREV, "collect"),
|
||||
gen_noop(), OP_HAS_VARIABLE);
|
||||
block_append(&c, array_var);
|
||||
|
||||
block tail = {0};
|
||||
block_append(&tail, gen_op_simple(DUP));
|
||||
block_append(&tail, gen_op_var_bound(LOADV, array_var));
|
||||
block_append(&tail, gen_op_simple(SWAP));
|
||||
block_append(&tail, gen_op_simple(APPEND));
|
||||
block_append(&tail, gen_op_var_bound(STOREV, array_var));
|
||||
block_append(&tail, gen_op_simple(BACKTRACK));
|
||||
|
||||
block_append(&c, gen_op_target(FORK, tail));
|
||||
block_append(&c, expr);
|
||||
block_append(&c, tail);
|
||||
|
||||
block_append(&c, gen_op_var_bound(LOADV, array_var));
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
block gen_assign(block expr) {
|
||||
block c = gen_noop();
|
||||
block_append(&c, gen_op_simple(DUP));
|
||||
block result_var = block_bind(gen_op_var_unbound(STOREV, "result"),
|
||||
gen_noop(), OP_HAS_VARIABLE);
|
||||
block_append(&c, result_var);
|
||||
|
||||
block loop = gen_noop();
|
||||
block_append(&loop, gen_op_simple(DUP));
|
||||
block_append(&loop, expr);
|
||||
block_append(&loop, gen_op_var_bound(ASSIGN, result_var));
|
||||
block_append(&loop, gen_op_simple(BACKTRACK));
|
||||
|
||||
block_append(&c, gen_op_target(FORK, loop));
|
||||
block_append(&c, loop);
|
||||
block_append(&c, gen_op_var_bound(LOADV, result_var));
|
||||
return c;
|
||||
}
|
||||
|
||||
block gen_definedor(block a, block b) {
|
||||
// var found := false
|
||||
block c = gen_op_simple(DUP);
|
||||
block_append(&c, gen_op_const(LOADK, jv_false()));
|
||||
block found_var = block_bind(gen_op_var_unbound(STOREV, "found"),
|
||||
gen_noop(), OP_HAS_VARIABLE);
|
||||
block_append(&c, found_var);
|
||||
|
||||
// if found, backtrack. Otherwise execute b
|
||||
block tail = gen_op_simple(DUP);
|
||||
block_append(&tail, gen_op_var_bound(LOADV, found_var));
|
||||
block backtrack = gen_op_simple(BACKTRACK);
|
||||
block_append(&tail, gen_op_target(JUMP_F, backtrack));
|
||||
block_append(&tail, backtrack);
|
||||
block_append(&tail, gen_op_simple(POP));
|
||||
block_append(&tail, b);
|
||||
|
||||
// try again
|
||||
block if_notfound = gen_op_simple(BACKTRACK);
|
||||
|
||||
// found := true, produce result
|
||||
block if_found = gen_op_simple(DUP);
|
||||
block_append(&if_found, gen_op_const(LOADK, jv_true()));
|
||||
block_append(&if_found, gen_op_var_bound(STOREV, found_var));
|
||||
block_append(&if_found, gen_op_target(JUMP, tail));
|
||||
|
||||
block_append(&c, gen_op_target(FORK, if_notfound));
|
||||
block_append(&c, a);
|
||||
block_append(&c, gen_op_target(JUMP_F, if_found));
|
||||
block_append(&c, if_found);
|
||||
block_append(&c, if_notfound);
|
||||
block_append(&c, tail);
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
block gen_condbranch(block iftrue, block iffalse) {
|
||||
block b = gen_noop();
|
||||
block_append(&iftrue, gen_op_target(JUMP, iffalse));
|
||||
block_append(&b, gen_op_target(JUMP_F, iftrue));
|
||||
block_append(&b, iftrue);
|
||||
block_append(&b, iffalse);
|
||||
return b;
|
||||
}
|
||||
|
||||
block gen_and(block a, block b) {
|
||||
// a and b = if a then (if b then true else false) else false
|
||||
block code = gen_op_simple(DUP);
|
||||
block_append(&code, a);
|
||||
|
||||
block if_a_true = gen_op_simple(POP);
|
||||
block_append(&if_a_true, b);
|
||||
block_append(&if_a_true, gen_condbranch(gen_op_const(LOADK, jv_true()),
|
||||
gen_op_const(LOADK, jv_false())));
|
||||
block_append(&code, gen_condbranch(if_a_true,
|
||||
block_join(gen_op_simple(POP), gen_op_const(LOADK, jv_false()))));
|
||||
return code;
|
||||
}
|
||||
|
||||
block gen_or(block a, block b) {
|
||||
// a or b = if a then true else (if b then true else false)
|
||||
block code = gen_op_simple(DUP);
|
||||
block_append(&code, a);
|
||||
|
||||
block if_a_false = gen_op_simple(POP);
|
||||
block_append(&if_a_false, b);
|
||||
block_append(&if_a_false, gen_condbranch(gen_op_const(LOADK, jv_true()),
|
||||
gen_op_const(LOADK, jv_false())));
|
||||
block_append(&code, gen_condbranch(block_join(gen_op_simple(POP), gen_op_const(LOADK, jv_true())),
|
||||
if_a_false));
|
||||
return code;
|
||||
}
|
||||
|
||||
block gen_cond(block cond, block iftrue, block iffalse) {
|
||||
block b = gen_op_simple(DUP);
|
||||
block_append(&b, cond);
|
||||
block_append(&b, gen_condbranch(block_join(gen_op_simple(POP), iftrue),
|
||||
block_join(gen_op_simple(POP), iffalse)));
|
||||
return b;
|
||||
}
|
||||
|
||||
block gen_cbinding(struct symbol_table* t, block code) {
|
||||
for (int cfunc=0; cfunc<t->ncfunctions; cfunc++) {
|
||||
inst* i = inst_new(CLOSURE_CREATE_C);
|
||||
i->imm.cfunc = &t->cfunctions[cfunc];
|
||||
i->symbol = strdup(i->imm.cfunc->name);
|
||||
code = block_bind(inst_block(i), code, OP_IS_CALL_PSEUDO);
|
||||
}
|
||||
return code;
|
||||
}
|
||||
|
||||
static uint16_t nesting_level(struct bytecode* bc, inst* target) {
|
||||
uint16_t level = 0;
|
||||
assert(bc && target->compiled);
|
||||
while (bc && target->compiled != bc) {
|
||||
level++;
|
||||
bc = bc->parent;
|
||||
}
|
||||
assert(bc && bc == target->compiled);
|
||||
return level;
|
||||
}
|
||||
|
||||
static int count_cfunctions(block b) {
|
||||
int n = 0;
|
||||
for (inst* i = b.first; i; i = i->next) {
|
||||
if (i->op == CLOSURE_CREATE_C) n++;
|
||||
if (opcode_describe(i->op)->flags & OP_HAS_BLOCK)
|
||||
n += count_cfunctions(i->subfn);
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
static int compile(struct locfile* locations, struct bytecode* bc, block b) {
|
||||
int errors = 0;
|
||||
int pos = 0;
|
||||
int var_frame_idx = 0;
|
||||
bc->nsubfunctions = 0;
|
||||
bc->nclosures = 0;
|
||||
for (inst* curr = b.first; curr; curr = curr->next) {
|
||||
if (!curr->next) assert(curr == b.last);
|
||||
pos += opcode_length(curr->op);
|
||||
curr->bytecode_pos = pos;
|
||||
curr->compiled = bc;
|
||||
|
||||
int opflags = opcode_describe(curr->op)->flags;
|
||||
if (opflags & OP_HAS_BINDING) {
|
||||
if (!curr->bound_by) {
|
||||
locfile_locate(locations, curr->source, "error: %s is not defined", curr->symbol);
|
||||
errors++;
|
||||
}
|
||||
}
|
||||
if ((opflags & OP_HAS_VARIABLE) &&
|
||||
curr->bound_by == curr) {
|
||||
curr->imm.intval = var_frame_idx++;
|
||||
}
|
||||
if (opflags & OP_HAS_BLOCK) {
|
||||
assert(curr->bound_by == curr);
|
||||
curr->imm.intval = bc->nsubfunctions++;
|
||||
}
|
||||
if (curr->op == CLOSURE_PARAM) {
|
||||
assert(curr->bound_by == curr);
|
||||
curr->imm.intval = bc->nclosures++;
|
||||
}
|
||||
if (curr->op == CLOSURE_CREATE_C) {
|
||||
assert(curr->bound_by == curr);
|
||||
int idx = bc->globals->ncfunctions++;
|
||||
bc->globals->cfunctions[idx] = *curr->imm.cfunc;
|
||||
curr->imm.intval = idx;
|
||||
}
|
||||
}
|
||||
if (bc->nsubfunctions) {
|
||||
bc->subfunctions = malloc(sizeof(struct bytecode*) * bc->nsubfunctions);
|
||||
for (inst* curr = b.first; curr; curr = curr->next) {
|
||||
if (!(opcode_describe(curr->op)->flags & OP_HAS_BLOCK))
|
||||
continue;
|
||||
struct bytecode* subfn = malloc(sizeof(struct bytecode));
|
||||
bc->subfunctions[curr->imm.intval] = subfn;
|
||||
subfn->globals = bc->globals;
|
||||
subfn->parent = bc;
|
||||
errors += compile(locations, subfn, curr->subfn);
|
||||
}
|
||||
} else {
|
||||
bc->subfunctions = 0;
|
||||
}
|
||||
bc->codelen = pos;
|
||||
uint16_t* code = malloc(sizeof(uint16_t) * bc->codelen);
|
||||
bc->code = code;
|
||||
pos = 0;
|
||||
jv constant_pool = jv_array();
|
||||
int maxvar = -1;
|
||||
if (!errors) for (inst* curr = b.first; curr; curr = curr->next) {
|
||||
const struct opcode_description* op = opcode_describe(curr->op);
|
||||
if (op->length == 0)
|
||||
continue;
|
||||
uint16_t* opcode_rewrite = &code[pos];
|
||||
code[pos++] = curr->op;
|
||||
int opflags = op->flags;
|
||||
assert(!(op->flags & OP_IS_CALL_PSEUDO));
|
||||
if (opflags & OP_HAS_VARIABLE_LENGTH_ARGLIST) {
|
||||
int nargs = curr->imm.intval;
|
||||
assert(nargs > 0);
|
||||
code[pos++] = (uint16_t)nargs;
|
||||
int desired_params = 0;
|
||||
for (int i=0; i<nargs; i++) {
|
||||
curr = curr->next;
|
||||
assert(curr && opcode_describe(curr->op)->flags & OP_IS_CALL_PSEUDO);
|
||||
code[pos++] = nesting_level(bc, curr->bound_by);
|
||||
switch (curr->bound_by->op) {
|
||||
default: assert(0 && "Unknown type of argument");
|
||||
case CLOSURE_CREATE:
|
||||
code[pos++] = curr->bound_by->imm.intval | ARG_NEWCLOSURE;
|
||||
if (i == 0) {
|
||||
inst* i = curr->bound_by;
|
||||
desired_params = i->compiled->subfunctions[i->imm.intval]->nclosures;
|
||||
}
|
||||
break;
|
||||
case CLOSURE_PARAM:
|
||||
code[pos++] = curr->bound_by->imm.intval;
|
||||
if (i == 0) desired_params = 0;
|
||||
break;
|
||||
case CLOSURE_CREATE_C:
|
||||
code[pos++] = curr->bound_by->imm.intval;
|
||||
*opcode_rewrite = bc->globals->cfunctions[curr->bound_by->imm.intval].callop;
|
||||
if (i == 0) desired_params = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
assert(nargs - 1 == desired_params);
|
||||
} else if (opflags & OP_HAS_CONSTANT) {
|
||||
code[pos++] = jv_array_length(jv_copy(constant_pool));
|
||||
constant_pool = jv_array_append(constant_pool, jv_copy(curr->imm.constant));
|
||||
} else if (opflags & OP_HAS_VARIABLE) {
|
||||
code[pos++] = nesting_level(bc, curr->bound_by);
|
||||
uint16_t var = (uint16_t)curr->bound_by->imm.intval;
|
||||
code[pos++] = var;
|
||||
if (var > maxvar) maxvar = var;
|
||||
} else if (opflags & OP_HAS_BRANCH) {
|
||||
assert(curr->imm.target->bytecode_pos != -1);
|
||||
assert(curr->imm.target->bytecode_pos > pos); // only forward branches
|
||||
code[pos] = curr->imm.target->bytecode_pos - (pos + 1);
|
||||
pos++;
|
||||
} else if (opflags & OP_HAS_CFUNC) {
|
||||
assert(curr->symbol);
|
||||
int found = 0;
|
||||
for (int i=0; i<bc->globals->ncfunctions; i++) {
|
||||
if (!strcmp(curr->symbol, bc->globals->cfunctions[i].name)) {
|
||||
code[pos++] = i;
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
assert(found);
|
||||
} else if (op->length > 1) {
|
||||
assert(0 && "codegen not implemented for this operation");
|
||||
}
|
||||
}
|
||||
bc->constants = constant_pool;
|
||||
bc->nlocals = maxvar + 2; // FIXME: frames of size zero?
|
||||
return errors;
|
||||
}
|
||||
|
||||
int block_compile(block b, struct locfile* locations, struct bytecode** out) {
|
||||
struct bytecode* bc = malloc(sizeof(struct bytecode));
|
||||
bc->parent = 0;
|
||||
bc->globals = malloc(sizeof(struct symbol_table));
|
||||
int ncfunc = count_cfunctions(b);
|
||||
bc->globals->ncfunctions = 0;
|
||||
bc->globals->cfunctions = malloc(sizeof(struct cfunction) * ncfunc);
|
||||
int nerrors = compile(locations, bc, b);
|
||||
assert(bc->globals->ncfunctions == ncfunc);
|
||||
if (nerrors > 0) {
|
||||
bytecode_free(bc);
|
||||
*out = 0;
|
||||
} else {
|
||||
*out = bc;
|
||||
}
|
||||
return nerrors;
|
||||
}
|
||||
|
||||
void block_free(block b) {
|
||||
struct inst* next;
|
||||
for (struct inst* curr = b.first; curr; curr = next) {
|
||||
next = curr->next;
|
||||
inst_free(curr);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
#ifndef COMPILE_H
|
||||
#define COMPILE_H
|
||||
#include <stdint.h>
|
||||
#include "bytecode.h"
|
||||
#include "opcode.h"
|
||||
#include "locfile.h"
|
||||
|
||||
struct inst;
|
||||
typedef struct inst inst;
|
||||
|
||||
|
||||
typedef struct block {
|
||||
inst* first;
|
||||
inst* last;
|
||||
} block;
|
||||
|
||||
block gen_location(location, block);
|
||||
|
||||
block gen_noop();
|
||||
block gen_op_simple(opcode op);
|
||||
block gen_op_const(opcode op, jv constant);
|
||||
block gen_op_target(opcode op, block target);
|
||||
block gen_op_var_unbound(opcode op, const char* name);
|
||||
block gen_op_var_bound(opcode op, block binder);
|
||||
block gen_op_block_defn(opcode op, const char* name, block block);
|
||||
block gen_op_block_defn_rec(opcode op, const char* name, block block);
|
||||
block gen_op_block_unbound(opcode op, const char* name);
|
||||
block gen_op_call(opcode op, block arglist);
|
||||
block gen_op_symbol(opcode op, const char* name);
|
||||
|
||||
block gen_subexp(block a);
|
||||
block gen_both(block a, block b);
|
||||
block gen_collect(block expr);
|
||||
block gen_assign(block expr);
|
||||
block gen_definedor(block a, block b);
|
||||
block gen_condbranch(block iftrue, block iffalse);
|
||||
block gen_and(block a, block b);
|
||||
block gen_or(block a, block b);
|
||||
|
||||
block gen_cond(block cond, block iftrue, block iffalse);
|
||||
|
||||
block gen_cbinding(struct symbol_table* functions, block b);
|
||||
|
||||
void block_append(block* b, block b2);
|
||||
block block_join(block a, block b);
|
||||
int block_has_only_binders(block, int bindflags);
|
||||
block block_bind(block binder, block body, int bindflags);
|
||||
|
||||
int block_compile(block, struct locfile*, struct bytecode**);
|
||||
|
||||
void block_free(block);
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,6 @@
|
||||
.DS_Store
|
||||
.sass-cache
|
||||
output/*
|
||||
|
||||
# Autogenerated from public/css/base.scss
|
||||
public/css/base.css
|
||||
@@ -0,0 +1,59 @@
|
||||
require 'bonsai'
|
||||
require 'liquid'
|
||||
require 'maruku'
|
||||
require 'json'
|
||||
|
||||
module ExtraFilters
|
||||
def markdownify(input)
|
||||
Maruku.new(input).to_html
|
||||
end
|
||||
|
||||
def sanitize(input)
|
||||
input.gsub(/[^a-zA-Z0-9_]/,"")
|
||||
end
|
||||
|
||||
def json(input)
|
||||
input.to_json
|
||||
end
|
||||
|
||||
def unique(input)
|
||||
@n = (@n || 0) + 1
|
||||
input + @n.to_s
|
||||
end
|
||||
end
|
||||
|
||||
Liquid::Template.register_filter(ExtraFilters)
|
||||
|
||||
|
||||
task :serve do
|
||||
begin
|
||||
Bonsai.log "Press Control+C to quit"
|
||||
|
||||
require 'rack'
|
||||
require 'sinatra'
|
||||
require 'watch'
|
||||
require 'launchy'
|
||||
|
||||
Bonsai.root_dir = Dir.pwd
|
||||
|
||||
server = fork {
|
||||
app = Rack::Builder.app {
|
||||
use Bonsai::StaticPassThrough, :root => Bonsai.root_dir + "/output", :urls => ["/"]
|
||||
run Bonsai::DevelopmentServer
|
||||
}
|
||||
Rack::Handler.default.run(app, :Port => 5000) do
|
||||
Launchy.open("http://localhost:5000/")
|
||||
end
|
||||
}
|
||||
Watch.new("{content,templates,public}/**/*") { Bonsai::Exporter.process! }
|
||||
rescue Interrupt
|
||||
Process.kill("QUIT", server)
|
||||
Process.wait(server)
|
||||
exit
|
||||
end
|
||||
end
|
||||
|
||||
task :build do
|
||||
Bonsai.root_dir = Dir.pwd
|
||||
Bonsai::Exporter.publish!
|
||||
end
|
||||
@@ -0,0 +1,241 @@
|
||||
headline: Tutorial
|
||||
body:
|
||||
- text: |
|
||||
|
||||
Twitter have a JSON API, so let's play with that. This URL gets
|
||||
us the last 5 tweets about JSON:
|
||||
|
||||
- command: "curl 'http://search.twitter.com/search.json?q=json&rpp=5&include_entities=true'"
|
||||
result: |
|
||||
{"completed_in":0.108,"max_id":247677287108067328,"max_id_str":"247677287108067328","next_page":"?page=2&max_id=247677287108067328&q=json&rpp=5&include_entities=1","page":1,"query":"json","refresh_url":"?since_id=247677287108067328&q=json&include_entities=1","results":[{"created_at":"Mon, 17 Sep 2012 12:44:01 +0000","entities":{"hashtags":[],"urls":[{"url":"http:\/\/t.co\/XRvh1ZVw","expanded_url":"http:\/\/jase.im\/Ri7I0M","display_url":"jase.im\/Ri7I0M","indices":[112,132]}],"user_mentions":[{"screen_name":"imagemechanics","name":"Jason Cotterell","id":57271393,"id_str":"57271393","indices":[3,18]}]},"from_user":"_AaronNg","from_user_id":79771704,"from_user_id_str":"79771704","from_user_name":"NgChenChong","geo":null,"id":247677287108067328,"id_str":"247677287108067328","iso_language_code":"en","metadata":{"result_type":"recent"},"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/2523558403\/ek8mo4j4beq84iw28gjl_normal.jpeg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/2523558403\/ek8mo4j4beq84iw28gjl_normal.jpeg","source":"<a href="http:\/\/twitter.com\/">web<\/a>","text":"RT @imagemechanics: iPhone 5 website teardown: How Apple compresses video using JPEG, JSON, and <canvas>.\nhttp:\/\/t.co\/XRvh1ZVw","to_user":null,"to_user_id":0,"to_user_id_str":"0","to_user_name":null}, ...
|
||||
|
||||
- text: |
|
||||
|
||||
There's lots of info and no whitespace, so to make it a bit more
|
||||
legible we pipe it through jq, telling jq to just spit the input
|
||||
back at us using the expression `.`:
|
||||
|
||||
|
||||
- command: "curl 'http://search.twitter.com/search.json?q=json&rpp=5&include_entities=true' | jq '.'"
|
||||
result: |
|
||||
|
||||
{
|
||||
"since_id_str": "0",
|
||||
"since_id": 0,
|
||||
"results_per_page": 5,
|
||||
"completed_in": 0.108,
|
||||
"max_id": 247677287108067330,
|
||||
"max_id_str": "247677287108067328",
|
||||
"next_page": "?page=2&max_id=247677287108067328&q=json&rpp=5&include_entities=1",
|
||||
"page": 1,
|
||||
"query": "json",
|
||||
"refresh_url": "?since_id=247677287108067328&q=json&include_entities=1",
|
||||
"results": [
|
||||
{
|
||||
"from_user_name": "NgChenChong",
|
||||
"from_user_id_str": "79771704",
|
||||
"from_user_id": 79771704,
|
||||
"from_user": "_AaronNg",
|
||||
"iso_language_code": "en",
|
||||
"text": "RT @imagemechanics: iPhone 5 website teardown: How Apple compresses video using JPEG, JSON, and <canvas>.\nhttp://t.co/XRvh1ZVw",
|
||||
"to_user": null
|
||||
/* lots more fields... */
|
||||
},
|
||||
/* lots more results... */
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
- text: |
|
||||
|
||||
Let's pull out the first tweet:
|
||||
|
||||
|
||||
- command: "curl 'http://search.twitter.com/search.json?q=json&rpp=5&include_entities=true' | jq '.results[0]'"
|
||||
result: |
|
||||
{
|
||||
"from_user_name": "NgChenChong",
|
||||
"from_user_id_str": "79771704",
|
||||
"from_user_id": 79771704,
|
||||
"from_user": "_AaronNg",
|
||||
"iso_language_code": "en",
|
||||
"text": "RT @imagemechanics: iPhone 5 website teardown: How Apple compresses video using JPEG, JSON, and <canvas>.\nhttp://t.co/XRvh1ZVw",
|
||||
"to_user": null
|
||||
/* lots more fields... */
|
||||
},
|
||||
|
||||
- text: |
|
||||
|
||||
For the rest of the examples, I'll leave out the `curl` command - it's not going to change.
|
||||
|
||||
|
||||
There's still a lot of info we don't care about there, so we'll
|
||||
restrict it down to the most interesting fields.
|
||||
|
||||
|
||||
- command: "jq '.results[0] | {from_user, text}'"
|
||||
result: |
|
||||
{
|
||||
"text": "RT @imagemechanics: iPhone 5 website teardown: How Apple compresses video using JPEG, JSON, and <canvas>.\nhttp://t.co/XRvh1ZVw",
|
||||
"from_user": "_AaronNg"
|
||||
}
|
||||
|
||||
- text: |
|
||||
|
||||
The `|` operator in jq feeds the output of one filter
|
||||
(`.results[0]` which gets the first element of the results
|
||||
array) into the input of another (`{from_user, text}` which
|
||||
builds an object of those two fields).
|
||||
|
||||
Now let's get the rest of the tweets:
|
||||
|
||||
|
||||
|
||||
- command: "jq '.results[] | {from_user, text}'"
|
||||
result: |
|
||||
{
|
||||
"text": "RT @imagemechanics: iPhone 5 website teardown: How Apple compresses video using JPEG, JSON, and <canvas>.\nhttp://t.co/XRvh1ZVw",
|
||||
"from_user": "_AaronNg"
|
||||
}
|
||||
{
|
||||
"text": "RT @_kud: iPhone 5 website teardown: How Apple compresses video using JPEG, JSON, and <canvas> -- http://t.co/Lhp92IqD",
|
||||
"from_user": "blouerat"
|
||||
}
|
||||
{
|
||||
"text": "Dynamic Forms Mobile App by pmadiset: Few Forms details are hosted on our server in the form of JSON. This JSON ... http://t.co/7KALdWaX",
|
||||
"from_user": "cebu_iphone"
|
||||
}
|
||||
{
|
||||
"text": "iPhone 5 website insanity - Video compressing using JPEG, JSON, and <canvas> http://t.co/28Jesbio (Oh #Apple, U So #Awesome...)",
|
||||
"from_user": "dieswaytoofast"
|
||||
}
|
||||
{
|
||||
"text": "RT @umutm: A very nice web-based JSON editor - http://t.co/M70snaIf",
|
||||
"from_user": "Leolik"
|
||||
}
|
||||
|
||||
|
||||
- text: |
|
||||
|
||||
`.results[]` returns each element of the results array, one at a
|
||||
time, which are all fed into `{from_user, text}`.
|
||||
|
||||
|
||||
Data in jq is represented as streams of JSON values - every jq
|
||||
expression runs for each value in its input stream, and can
|
||||
produce any number of values to its output stream.
|
||||
|
||||
Streams are serialised by just separating JSON values with
|
||||
whitespace. This is a `cat`-friendly format - you can just join
|
||||
two JSON streams together and get a valid JSON stream.
|
||||
|
||||
If you want to get the output as a single array, you can tell jq to
|
||||
"collect" all of the answers by wrapping the filter in square
|
||||
brackets:
|
||||
|
||||
- command: "jq '[.results[] | {from_user, text}]'"
|
||||
result: |
|
||||
[
|
||||
{
|
||||
"text": "RT @imagemechanics: iPhone 5 website teardown: How Apple compresses video using JPEG, JSON, and <canvas>.\nhttp://t.co/XRvh1ZVw",
|
||||
"from_user": "_AaronNg"
|
||||
},
|
||||
{
|
||||
"text": "RT @_kud: iPhone 5 website teardown: How Apple compresses video using JPEG, JSON, and <canvas> -- http://t.co/Lhp92IqD",
|
||||
"from_user": "blouerat"
|
||||
},
|
||||
{
|
||||
"text": "Dynamic Forms Mobile App by pmadiset: Few Forms details are hosted on our server in the form of JSON. This JSON ... http://t.co/7KALdWaX",
|
||||
"from_user": "cebu_iphone"
|
||||
},
|
||||
{
|
||||
"text": "iPhone 5 website insanity - Video compressing using JPEG, JSON, and <canvas> http://t.co/28Jesbio (Oh #Apple, U So #Awesome...)",
|
||||
"from_user": "dieswaytoofast"
|
||||
},
|
||||
{
|
||||
"text": "RT @umutm: A very nice web-based JSON editor - http://t.co/M70snaIf",
|
||||
"from_user": "Leolik"
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
- text: |
|
||||
|
||||
- - -
|
||||
|
||||
Next, let's try getting the URLs out of those API results as
|
||||
well. In each tweet, the Twitter API includes a field called
|
||||
"entities" which looks like this:
|
||||
|
||||
{
|
||||
"user_mentions": [],
|
||||
"urls": [{
|
||||
"indices": [83, 103],
|
||||
"display_url": "bit.ly/StniqT",
|
||||
"expanded_url": "http://bit.ly/StniqT",
|
||||
"url": "http://t.co/28Jesbio"
|
||||
}],
|
||||
"hashtags": [
|
||||
{"indices": [108, 114], "text": "Apple" },
|
||||
{"indices": [121, 129], "text": "Awesome"}
|
||||
]
|
||||
}
|
||||
|
||||
We want to pull out all of the "url" fields inside that array of url
|
||||
objects, and make a simple list of strings to go along with the
|
||||
"from_user" and "text" fields we already have.
|
||||
|
||||
|
||||
|
||||
- command: "jq '.results[] | {from_user, text, urls: [.entities.urls[].url]}'"
|
||||
result: |
|
||||
{
|
||||
"urls": [
|
||||
"http://t.co/XRvh1ZVw"
|
||||
],
|
||||
"text": "RT @imagemechanics: iPhone 5 website teardown: How Apple compresses video using JPEG, JSON, and <canvas>.\nhttp://t.co/XRvh1ZVw",
|
||||
"from_user": "_AaronNg"
|
||||
}
|
||||
{
|
||||
"urls": [
|
||||
"http://t.co/Lhp92IqD"
|
||||
],
|
||||
"text": "RT @_kud: iPhone 5 website teardown: How Apple compresses video using JPEG, JSON, and <canvas> -- http://t.co/Lhp92IqD",
|
||||
"from_user": "blouerat"
|
||||
}
|
||||
{
|
||||
"urls": [
|
||||
"http://t.co/7KALdWaX"
|
||||
],
|
||||
"text": "Dynamic Forms Mobile App by pmadiset: Few Forms details are hosted on our server in the form of JSON. This JSON ... http://t.co/7KALdWaX",
|
||||
"from_user": "cebu_iphone"
|
||||
}
|
||||
{
|
||||
"urls": [
|
||||
"http://t.co/28Jesbio"
|
||||
],
|
||||
"text": "iPhone 5 website insanity - Video compressing using JPEG, JSON, and <canvas> http://t.co/28Jesbio (Oh #Apple, U So #Awesome...)",
|
||||
"from_user": "dieswaytoofast"
|
||||
}
|
||||
{
|
||||
"urls": [
|
||||
"http://t.co/M70snaIf"
|
||||
],
|
||||
"text": "RT @umutm: A very nice web-based JSON editor - http://t.co/M70snaIf",
|
||||
"from_user": "Leolik"
|
||||
}
|
||||
|
||||
|
||||
- text: |
|
||||
|
||||
Here we're making an object as before, but this time the urls
|
||||
field is being set to `[.entities.urls[].url]`, which collects
|
||||
all of the URLs defined in the entities object.
|
||||
|
||||
- text: |
|
||||
|
||||
- - -
|
||||
|
||||
Here endeth the tutorial! There's lots more to play with, go
|
||||
read [the manual](../manual) if you're interested, and [download
|
||||
jq](../download) if you haven't already.
|
||||
@@ -0,0 +1,57 @@
|
||||
headline: Download jq
|
||||
body:
|
||||
|
||||
- text: |
|
||||
|
||||
jq is written in C and has no runtime dependencies, so it should be
|
||||
possible to build it for nearly any platform. Prebuilt binaries are
|
||||
available for Linux (64-bit x86) and OS X.
|
||||
|
||||
* [Download binary for 64-bit Linux](linux_x86_64/jq)
|
||||
* [Download binary for OS X](osx_64/jq)
|
||||
* [Download source](source/jq.tgz)
|
||||
|
||||
The binaries should just run, but you may need to make them
|
||||
executable first using:
|
||||
|
||||
chmod +x jq
|
||||
|
||||
jq is licensed under the MIT license. For all of the gory
|
||||
details, read the file `COPYING` in the source distribution.
|
||||
|
||||
Hacking on jq
|
||||
=============
|
||||
|
||||
If you want to work on jq, grab the source from
|
||||
[https://github.com/stedolan/jq](https://github.com/stedolan/jq).
|
||||
|
||||
|
||||
To build it from a git clone, you'll need to install a few
|
||||
packages first:
|
||||
|
||||
* [Flex](http://www.gnu.org/software/flex/)
|
||||
* [Bison](http://www.gnu.org/software/bison/)
|
||||
* [Python](http://www.python.org)
|
||||
* [GCC](http://gcc.gnu.org)
|
||||
* [Make](http://www.gnu.org/software/make)
|
||||
|
||||
Most of these aren't necessary if you're just trying to compile
|
||||
jq from the released tarball - that version has the
|
||||
non-platform-specific build steps already done, so you'll only
|
||||
need a C compiler and `make` to finish it off.
|
||||
|
||||
For Linux systems, these will all be in your system's package
|
||||
manager, and if you do development on the machine they're most
|
||||
likely already installed. I have no idea how to get these
|
||||
installed on OS X, you're on your own there.
|
||||
|
||||
`flex` and `bison` are used to generate the lexer and parser for
|
||||
jq, and some python scripts generate the UTF8 encoding tables
|
||||
needed for JSON parsing.
|
||||
|
||||
Building the documentation
|
||||
--
|
||||
|
||||
jq's documentation is compiled into static HTML using
|
||||
[Bonsai](http://www.tinytree.info). To view the documentation
|
||||
locally, run `rake serve` from the docs/ subdirectory.
|
||||
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
@@ -0,0 +1,804 @@
|
||||
headline: jq Manual
|
||||
body: |
|
||||
|
||||
A jq program is a "filter": it takes an input, and produces an
|
||||
output. There are a lot of builtin filters for extracting a
|
||||
particular field of an object, or converting a number to a string,
|
||||
or various other standard tasks.
|
||||
|
||||
Filters can be combined in various ways - you can pipe the output of
|
||||
one filter into another filter, or collect the output of a filter
|
||||
into an array.
|
||||
|
||||
Some filters produce multiple results, for instance there's one that
|
||||
produces all the elements of its input array. Piping that filter
|
||||
into a second runs the second filter for each element of the
|
||||
array. Generally, things that would be done with loops and iteration
|
||||
in other languages are just done by gluing filters together in jq.
|
||||
|
||||
It's important to remember that every filter has an input and an
|
||||
output. Even literals like "hello" or 42 are filters - they take an
|
||||
input but always produce the same literal as output. Operations that
|
||||
combine two filters, like addition, generally feed the same input to
|
||||
both and combine the results. So, you can implement an averaging
|
||||
filter as `add / length` - feeding the input array both to the `add`
|
||||
filter and the `length` filter and dividing the results.
|
||||
|
||||
But that's getting ahead of ourselves. :) Let's start with something
|
||||
simpler:
|
||||
|
||||
sections:
|
||||
- title: Invoking jq
|
||||
body: |
|
||||
|
||||
jq filters run on a stream of JSON data. The input to jq is
|
||||
parsed as a sequence of whitespace-separated JSON values which
|
||||
are passed through the provided filter one at a time. The
|
||||
output(s) of the filter are written to standard out, again as a
|
||||
sequence of whitespace-separated JSON data.
|
||||
|
||||
You can affect how jq reads and writes its input and output
|
||||
using some command-line options:
|
||||
|
||||
* `--slurp`/`-s`
|
||||
|
||||
Instead of running the filter for each JSON object in the
|
||||
input, read the entire input stream into a large array and run
|
||||
the filter just once.
|
||||
|
||||
* `--raw-input`/`-R`
|
||||
|
||||
Don't parse the input as JSON. Instead, each line of text is
|
||||
passed to the filter as a string. If combined with `--slurp`,
|
||||
then the entire input is passed to the filter as a single long
|
||||
string.
|
||||
|
||||
* `--null-input`/`-n`
|
||||
|
||||
Don't read any input at all! Instead, the filter is run once
|
||||
using `null` as the input. This is useful when using jq as a
|
||||
simple calculator or to construct JSON data from scratch.
|
||||
|
||||
* `--compact-output` / `-c`
|
||||
|
||||
By default, jq pretty-prints JSON output. Using this option
|
||||
will result in more compact output by instead putting each
|
||||
JSON object on a single line.
|
||||
|
||||
* `--ascii-output` / `-a`
|
||||
|
||||
jq usually outputs non-ASCII Unicode codepoints as UTF-8, even
|
||||
if the input specified them as escape sequences (like
|
||||
"\u03bc"). Using this option, you can force jq to produce pure
|
||||
ASCII output with every non-ASCII character replaced with the
|
||||
equivalent escape sequence.
|
||||
|
||||
* `--raw-output` / `-r`
|
||||
|
||||
With this option, if the filter's result is a string then it
|
||||
will be written directly to standard output rather than being
|
||||
formatted as a JSON string with quotes. This can be useful for
|
||||
making jq filters talk to non-JSON-based systems.
|
||||
|
||||
- title: Basic filters
|
||||
entries:
|
||||
- title: "`.`"
|
||||
body: |
|
||||
|
||||
The absolute simplest (and least interesting) filter
|
||||
is `.`. This is a filter that takes its input and
|
||||
produces it unchanged as output.
|
||||
|
||||
Since jq by default pretty-prints all output, this trivial
|
||||
program can be a useful way of formatting JSON output from,
|
||||
say, `curl`.
|
||||
|
||||
examples:
|
||||
- program: '.'
|
||||
input: '"Hello, world!"'
|
||||
output: ['"Hello, world!"']
|
||||
|
||||
- title: "`.foo`"
|
||||
body: |
|
||||
|
||||
The simplest *useful* filter is .foo. When given a
|
||||
JSON object (aka dictionary or hash) as input, it produces
|
||||
the value at the key "foo", or null if there\'s none present.
|
||||
|
||||
examples:
|
||||
- program: '.foo'
|
||||
input: '{"foo": 42, "bar": "less interesting data"}'
|
||||
output: [42]
|
||||
- program: '.foo'
|
||||
input: '{"notfoo": true, "alsonotfoo": false}'
|
||||
output: ['null']
|
||||
|
||||
- title: "`.[foo]`"
|
||||
body: |
|
||||
|
||||
You can also look up fields of an object using syntax like
|
||||
`.["foo"]` (.foo above is a shorthand version of this). This
|
||||
one works for arrays as well, if the key is an
|
||||
integer. Arrays are zero-based (like javascript), so .[2]
|
||||
returns the third element of the array.
|
||||
|
||||
examples:
|
||||
- program: '.[0]'
|
||||
input: '[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]'
|
||||
output: ['{"name":"JSON", "good":true}']
|
||||
|
||||
- program: '.[2]'
|
||||
input: '[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]'
|
||||
output: ['null']
|
||||
|
||||
- title: "`.[]`"
|
||||
body: |
|
||||
|
||||
If you use the `.[foo]` syntax, but omit the index
|
||||
entirely, it will return *all* of the elements of an
|
||||
array. Running `.[]` with the input `[1,2,3]` will produce the
|
||||
numbers as three seperate results, rather than as a single
|
||||
array.
|
||||
|
||||
examples:
|
||||
- program: '.[]'
|
||||
input: '[{name":"JSON", "good":true}, {"name":"XML", "good":false}]'
|
||||
output:
|
||||
- '{"name":"JSON", "good":true}'
|
||||
- '{"name":"XML", "good":false}'
|
||||
|
||||
- program: '.[]'
|
||||
input: '[]'
|
||||
output: []
|
||||
|
||||
- title: "`,`"
|
||||
body: |
|
||||
|
||||
If two filters are separated by a comma, then the
|
||||
input will be fed into both and there will be multiple
|
||||
outputs: first, all of the outputs produced by the left
|
||||
expression, and then all of the outputs produced by the
|
||||
right. For instance, filter `.foo, .bar`, produces
|
||||
both the "foo" fields and "bar" fields as separate outputs.
|
||||
|
||||
examples:
|
||||
- program: '.foo, .bar'
|
||||
input: '{"foo": 42, "bar": "something else", "baz": true}'
|
||||
output: ['42', '"something else"']
|
||||
|
||||
- program: ".user, .projects[]"
|
||||
input: '{"user":"stedolan", "projects": ["jq", "wikiflow"]}'
|
||||
output: ['"stedolan"', '"jq"', '"wikiflow"']
|
||||
|
||||
- program: '.[4,2]'
|
||||
input: '["a","b","c","d","e"]'
|
||||
output: ['"d"', '"c"']
|
||||
|
||||
- title: "`|`"
|
||||
body: |
|
||||
The | operator combines two filters by feeding the output(s) of
|
||||
the one on the left into the input of the one on the right. It\'s
|
||||
pretty much the same as the Unix shell\'s pipe, if you\'re used to
|
||||
that.
|
||||
|
||||
If the one on the left produces multiple results, the one on
|
||||
the right will be run for each of those results. So, the
|
||||
expression `.[] | .foo` retrieves the "foo" field of each
|
||||
element of the input array.
|
||||
|
||||
examples:
|
||||
- program: '.[] | .name'
|
||||
input: '[{name":"JSON", "good":true}, {"name":"XML", "good":false}]'
|
||||
output: ['"JSON"', '"XML"']
|
||||
|
||||
- title: Types and Values
|
||||
body: |
|
||||
|
||||
jq supports the same set of datatypes as JSON - numbers,
|
||||
strings, booleans, arrays, objects (which in JSON-speak are
|
||||
hashes with only string keys), and "null".
|
||||
|
||||
Booleans, null, strings and numbers are written the same way as
|
||||
in javascript. Just like everything else in jq, these simple
|
||||
values take an input and produce an output - `42` is a valid jq
|
||||
expression that takes an input, ignores it, and returns 42
|
||||
instead.
|
||||
|
||||
entries:
|
||||
- title: Array construction - `[]`
|
||||
body: |
|
||||
|
||||
As in JSON, `[]` is used to construct arrays, as in
|
||||
`[1,2,3]`. The elements of the arrays can be any jq
|
||||
expression. All of the results produced by all of the
|
||||
expressions are collected into one big array. You can use it
|
||||
to construct an array out of a known quantity of values (as
|
||||
in `[.foo, .bar, .baz]`) or to "collect" all the results of a
|
||||
filter into an array (as in `[.items[].name]`)
|
||||
|
||||
Once you understand the "," operator, you can look at jq\'s array
|
||||
syntax in a different light: the expression [1,2,3] is not using a
|
||||
built-in syntax for comma-separated arrays, but is instead applying
|
||||
the `[]` operator (collect results) to the expression 1,2,3 (which
|
||||
produces three different results).
|
||||
|
||||
If you have a filter `X` that produces four results,
|
||||
then the expression `[X]` will produce a single result, an
|
||||
array of four elements.
|
||||
|
||||
examples:
|
||||
- program: "[.user, .projects[]]"
|
||||
input: '{"user":"stedolan", "projects": ["jq", "wikiflow"]}'
|
||||
output: ['["stedolan", "jq", "wikiflow"]']
|
||||
- title: Objects - `{}`
|
||||
body: |
|
||||
|
||||
Like JSON, `{}` is for constructing objects (aka
|
||||
dictionaries or hashes), as in: `{"a": 42, "b": 17}`.
|
||||
|
||||
If the keys are "sensible" (all alphabetic characters), then
|
||||
the quotes can be left off. The value can be any expression
|
||||
(although you may need to wrap it in parentheses if it\'s a
|
||||
complicated one), which gets applied to the {} expression\'s
|
||||
input (remember, all filters have an input and an
|
||||
output).
|
||||
|
||||
{foo: .bar}
|
||||
|
||||
will produce the JSON object `{"foo": 42}` if given the JSON
|
||||
object `{"bar":42, "baz":43}`. You can use this to select
|
||||
particular fields of an object: if the input is an object
|
||||
with "user", "title", "id", and "content" fields and you
|
||||
just want "user" and "title", you can write
|
||||
|
||||
{user: .user, title: .title}
|
||||
|
||||
Because that\'s so common, there\'s a shortcut syntax: `{user, title}`.
|
||||
|
||||
If one of the expressions produces multiple results,
|
||||
multiple dictionaries will be produced. If the input\'s
|
||||
|
||||
{"user":"stedolan","titles":["JQ Primer", "More JQ"]}
|
||||
|
||||
then the expression
|
||||
|
||||
{user, title: .titles[]}
|
||||
|
||||
will produce two outputs:
|
||||
|
||||
{"user":"stedolan", "title": "JQ Primer"}
|
||||
{"user":"stedolan", "title": "More JQ"}
|
||||
|
||||
Putting parentheses around the key means it will be evaluated as an
|
||||
expression. With the same input as above,
|
||||
|
||||
{(.user): .titles}
|
||||
|
||||
produces
|
||||
|
||||
{"stedolan": ["JQ Primer", "More JQ"]}
|
||||
|
||||
examples:
|
||||
- program: '{user, title: .titles[]}'
|
||||
input: '{"user":"stedolan","titles":["JQ Primer", "More JQ"]}'
|
||||
output:
|
||||
- '{"user":"stedolan", "title": "JQ Primer"}'
|
||||
- '{"user":"stedolan", "title": "More JQ"}'
|
||||
- program: '{(.user): .titles}'
|
||||
input: '{"user":"stedolan","titles":["JQ Primer", "More JQ"]}'
|
||||
output: ['{"stedolan": ["JQ Primer", "More JQ"]}']
|
||||
|
||||
- title: Builtin operators and functions
|
||||
body: |
|
||||
|
||||
Some jq operator (for instance, `+`) do different things
|
||||
depending on the type of their arguments (arrays, numbers,
|
||||
etc.). However, jq never does implicit type conversions. If you
|
||||
try to add a string to an object you'll get an error message and
|
||||
no result.
|
||||
|
||||
entries:
|
||||
- title: Addition - `+`
|
||||
body: |
|
||||
|
||||
The operator `+` takes two filters, applies them both
|
||||
to the same input, and adds the results together. What
|
||||
"adding" means depends on the types involved:
|
||||
|
||||
- **Numbers** are added by normal arithmetic.
|
||||
|
||||
- **Arrays** are added by being concatenated into a larger array.
|
||||
|
||||
- **Strings** are added by being joined into a larger string.
|
||||
|
||||
- **Objects** are added by merging, that is, inserting all
|
||||
the key-value pairs from both objects into a single
|
||||
combined object. If both objects contain a value for the
|
||||
same key, the object on the right of the `+` wins.
|
||||
|
||||
examples:
|
||||
- program: '.a + 1'
|
||||
input: '{"a": 7}'
|
||||
output: '{"a": 8}'
|
||||
- program: '.a + .b'
|
||||
input: '{"a": [1,2], "b": [3,4]}'
|
||||
output: ['[1,2,3,4]']
|
||||
- program: '{a: 1} + {b: 2} + {c: 3} + {a: 42}'
|
||||
input: 'null'
|
||||
output: ['{"a": 42, "b": 2, "c": 3}']
|
||||
|
||||
- title: Subtraction - `-`
|
||||
body: |
|
||||
|
||||
As well as normal arithmetic subtraction on numbers, the `-`
|
||||
operator can be used on arrays to remove all occurences of
|
||||
the second array's elements from the first array.
|
||||
|
||||
examples:
|
||||
- program: '4 - .a'
|
||||
input: '{"a":3}'
|
||||
output: ['1']
|
||||
- program: . - ["xml", "yaml"]
|
||||
input: '["xml", "yaml", "json"]'
|
||||
output: ['["json"]']
|
||||
|
||||
- title: Multiplication, division - `*` and `/`
|
||||
body: |
|
||||
|
||||
These operators only work on numbers, and do the expected.
|
||||
|
||||
examples:
|
||||
- program: '10 / . * 3'
|
||||
input: 5
|
||||
output: [6]
|
||||
|
||||
- title: `length`
|
||||
body: |
|
||||
|
||||
The builtin function `length` gets the length of various
|
||||
different types of value:
|
||||
|
||||
- The length of a **string** is the number of Unicode
|
||||
codepoints it contains (which will be the same as its
|
||||
JSON-encoded length in bytes if it's pure ASCII).
|
||||
|
||||
- The length of an **array** is the number of elements.
|
||||
|
||||
- The length of an **object** is the number of key-value pairs.
|
||||
|
||||
- The length of **null** is zero.
|
||||
|
||||
examples:
|
||||
- program: '.[] | length'
|
||||
input: '[[1,2], "string", {"a":2}, null]'
|
||||
output: [2, 6, 1, 0]
|
||||
|
||||
- title: `map(x)`
|
||||
body: |
|
||||
|
||||
For any filter `x`, `map(x)` will run that filter for each
|
||||
element of the input array, and produce the outputs a new
|
||||
array. `map(.+1)` will increment each element of an array of numbers.
|
||||
|
||||
`map(x)` is equivalent to `[.[] | x]`. In fact, this is how
|
||||
it's defined.
|
||||
|
||||
examples:
|
||||
- program: 'map(.+1)'
|
||||
input: '[1,2,3]'
|
||||
output: ['[2,3,4]']
|
||||
|
||||
- title: `add`
|
||||
body: |
|
||||
|
||||
The filter `add` takes as input an array, and produces as
|
||||
output the elements of the array added together. This might
|
||||
mean summed, concatenated or merged depending on the types
|
||||
of the elements of the input array - the rules are the same
|
||||
as those for the `+` operator (described above).
|
||||
|
||||
If the input is an empty array, `add` returns `null`.
|
||||
|
||||
examples:
|
||||
- program: add
|
||||
input: '["a","b","c"]'
|
||||
output: ["abc"]
|
||||
- program: add
|
||||
input: '[1, 2, 3]'
|
||||
output: [6]
|
||||
- program: add
|
||||
input: '[]'
|
||||
output: ["null"]
|
||||
|
||||
- title: `tonumber`
|
||||
body: |
|
||||
|
||||
The `tonumber` function parses its input as a number. It
|
||||
will convert correctly-formatted strings to their numeric
|
||||
equivalent, leave numbers alone, and give an error on all other input.
|
||||
|
||||
examples:
|
||||
- program: '.[] | tonumber'
|
||||
input: '[1, "1"]'
|
||||
output: [1,1]
|
||||
|
||||
- title: `tostring`
|
||||
body: |
|
||||
|
||||
The `tostring` function prints its input as a
|
||||
string. Strings are left unchanged, and all other values are
|
||||
JSON-encoded.
|
||||
|
||||
examples:
|
||||
- program: '.[] | tostring'
|
||||
input: '[1, "1", [1]]'
|
||||
output: ['"1"', '"1"', '"[1]"']
|
||||
|
||||
- title: "String interpolation - `@(text)`"
|
||||
body: |
|
||||
|
||||
jq supports an alternative syntax for strings. Instead of
|
||||
"foo", you can write `@(foo)`. When using this syntax,
|
||||
`%(expression)` may be used to insert the value of
|
||||
`expression` into the string (converted with `tostring`).
|
||||
|
||||
String interpolation does not occur for normal double-quoted
|
||||
strings (like `"foo"`) in order to be fully compatible with
|
||||
JSON.
|
||||
|
||||
All of the usual JSON escapes (`\n`, `\r` and the like) work
|
||||
inside `@()`-quoted strings, as well as `\%` and `\)` if
|
||||
those characters are needed literally.
|
||||
|
||||
examples:
|
||||
- program: '@(The input was %(.), which is one less than %(.+1))'
|
||||
input: '42'
|
||||
output: ['"The input was 42, which is one less than 43"']
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
- title: Conditionals and Comparisons
|
||||
entries:
|
||||
- title: `==`, `!=`
|
||||
body: |
|
||||
|
||||
The expression 'a == b' will produce 'true' if the result of a and b
|
||||
are equal (that is, if they represent equivalent JSON documents) and
|
||||
'false' otherwise. In particular, strings are never considered equal
|
||||
to numbers. If you're coming from Javascript, jq's == is like
|
||||
Javascript's === - considering values equal only when they have the
|
||||
same type as well as the same value.
|
||||
|
||||
!= is "not equal", and 'a != b' returns the opposite value of 'a == b'
|
||||
|
||||
examples:
|
||||
- program: '.[] == 1'
|
||||
input: '[1, 1.0, "1", "banana"]'
|
||||
output: ['[true, true, false, false]']
|
||||
|
||||
- title: if-then-else
|
||||
body: |
|
||||
|
||||
`if A then B else C end` will act the same as `B` if `A`
|
||||
produces a value other than false or null, but act the same
|
||||
as `C` otherwise.
|
||||
|
||||
Checking for false or null is a simpler notion of
|
||||
"truthiness" than is found in Javascript or Python, but it
|
||||
means that you'll sometimes have to be more explicit about
|
||||
the condition you want: you can't test whether, e.g. a
|
||||
string is empty using `if .name then A else B end`, you'll
|
||||
need something more like 'if (.name | count) > 0 then A else
|
||||
B end' instead.
|
||||
|
||||
If the condition A produces multiple results, it is
|
||||
considered "true" if any of those results is not false or
|
||||
null. If it produces zero results, it's considered false.
|
||||
|
||||
More cases can be added to an if using `elif A then B` syntax.
|
||||
|
||||
examples:
|
||||
- program: |-
|
||||
if . == 0 then
|
||||
"zero"
|
||||
elif . == 1 then
|
||||
"one"
|
||||
else
|
||||
"many"
|
||||
end
|
||||
input: 2
|
||||
output: ['"many"']
|
||||
|
||||
- title: and/or/not
|
||||
body: |
|
||||
|
||||
jq supports the normal Boolean operators and/or/not. They have the
|
||||
same standard of truth as if expressions - false and null are
|
||||
considered "false values", and anything else is a "true value".
|
||||
|
||||
If an operand of one of these operators produces multiple
|
||||
results, the operator itself will produce a result for each input.
|
||||
|
||||
`not` is in fact a builtin function rather than an operator,
|
||||
so it is called as a filter to which things can be piped
|
||||
rather than with special syntax, as in `.foo and .bar |
|
||||
not`.
|
||||
|
||||
These three only produce the values "true" and "false", and
|
||||
so are only useful for genuine Boolean operations, rather
|
||||
than the common Perl/Python/Ruby idiom of
|
||||
"value_that_may_be_null or default". If you want to use this
|
||||
form of "or", picking between two values rather than
|
||||
evaluating a condition, see the "//" operator below.
|
||||
|
||||
examples:
|
||||
- program: '42 and "a string"'
|
||||
input: 'null'
|
||||
output: ['true']
|
||||
- program: '(true, false) or false'
|
||||
input: 'null'
|
||||
output: ['true', 'false']
|
||||
- program: '(true, false) and (true, false)'
|
||||
input: 'null'
|
||||
output: ['true', 'false', 'false', 'false']
|
||||
- program: '[true, false | not]'
|
||||
input: 'null'
|
||||
output: ['[false, true]']
|
||||
|
||||
- title: Alternative operator - `//`
|
||||
body: |
|
||||
|
||||
A filter of the form `a // b` produces the same
|
||||
results as `a`, if `a` produces results other than `false`
|
||||
and `null`. Otherwise, `a // b` produces the same results as `b`.
|
||||
|
||||
This is useful for providing defaults: `.foo // 1` will
|
||||
evaluate to `1` if there's no `.foo` element in the
|
||||
input. It's similar to how `or` is sometimes used in Python
|
||||
(jq's `or` operator is reserved for strictly Boolean
|
||||
operations).
|
||||
|
||||
examples:
|
||||
- program: '.foo // 42'
|
||||
input: '{"foo": 19}'
|
||||
output: [19]
|
||||
- program: '.foo // 42'
|
||||
input: '{}'
|
||||
output: [42]
|
||||
|
||||
- title: Variables and Functions
|
||||
body: |
|
||||
Variables are an absolute necessity in most programming languages, but
|
||||
they're relegated to an "advanced feature" in jq.
|
||||
|
||||
In most languages, variables are the only means of passing around
|
||||
data. If you calculate a value, and you want to use it more than once,
|
||||
you'll need to store it in a variable. To pass a value to another part
|
||||
of the program, you'll need that part of the program to define a
|
||||
variable (as a function parameter, object member, or whatever) in
|
||||
which to place the data.
|
||||
|
||||
It is also possible to define functions in jq, although this is
|
||||
is a feature whose biggest use is defining jq's standard library
|
||||
(many jq functions such as `map` and `find` are in fact written
|
||||
in jq).
|
||||
|
||||
entries:
|
||||
- title: Variables
|
||||
body: |
|
||||
|
||||
In jq, all filters have an input and an output, so manual
|
||||
plumbing is not necessary to pass a value from one part of a program
|
||||
to the next. Many expressions, for instance `a + b`, pass their input
|
||||
to two distinct subexpressions (here `a` and `b` are both passed the
|
||||
same input), so variables aren't usually necessary in order to use a
|
||||
value twice.
|
||||
|
||||
For instance, calculating the average value of an array of numbers
|
||||
requires a few variables in most languages - at least one to hold the
|
||||
array, perhaps one for each element or for a loop counter. In jq, it's
|
||||
simply `add / length` - the `sum` expression is given the array and
|
||||
produces its sum, and the `count` expression is given the array and
|
||||
produces its length.
|
||||
|
||||
So, there's generally a cleaner way to solve most problems in jq that
|
||||
defining variables. Still, sometimes they do make things easier, so jq
|
||||
lets you define variables using `expression as $variable`. All
|
||||
variable names start with `$`. Here's a slightly uglier version of the
|
||||
array-averaging example:
|
||||
|
||||
length as $array_length | add / $array_length
|
||||
|
||||
We'll need a more complicated problem to find a situation where using
|
||||
variables actually makes our lives easier.
|
||||
|
||||
|
||||
Suppose we have an array of blog posts, with "author" and "title"
|
||||
fields, and another object which is used to map author usernames to
|
||||
real names. Our input looks like:
|
||||
|
||||
{"posts": [{"title": "Frist psot", "author": "anon"},
|
||||
{"title": "A well-written article", "author": "person1"}],
|
||||
"realnames": {"anon": "Anonymous Coward",
|
||||
"person1": "Person McPherson"}}
|
||||
|
||||
We want to produce the posts with the author field containing a real
|
||||
name, as in:
|
||||
|
||||
{"title": "Frist psot", "author": "Anonymous Coward"}
|
||||
{"title": "A well-written article", "author": "Person McPherson"}
|
||||
|
||||
We use a variable, $names, to store the realnames object, so that we
|
||||
can refer to it later when looking up author usernames:
|
||||
|
||||
.realnames as $names | .posts[] | {title, author: $names[.author]}
|
||||
|
||||
The expression "foo as $x" runs foo, puts the result in $x,
|
||||
and returns the original input. Apart from the side-effect
|
||||
of binding the variable, it has the same effect as ".".
|
||||
|
||||
Variables are scoped over the rest of the expression that defines
|
||||
them, so
|
||||
|
||||
.realnames as $names | (.posts[] | {title, author: $names[.author]})
|
||||
|
||||
will work, but
|
||||
|
||||
(.realnames as $names | .posts[]) | {title, author: $names[.author]}
|
||||
|
||||
won't.
|
||||
|
||||
examples:
|
||||
- program: '.bar as $x | .foo | . + $x'
|
||||
input: '{"foo":10, "bar":200}'
|
||||
output: ['210']
|
||||
|
||||
- title: 'Defining Functions'
|
||||
body: |
|
||||
|
||||
You can give a filter a name using "def" syntax:
|
||||
|
||||
def increment: . + 1;
|
||||
|
||||
From then on, `increment` is usable as a filter just like a
|
||||
builtin function (in fact, this is how some of the builtins
|
||||
are defined). A function may take arguments:
|
||||
|
||||
def map(f): [.[] | f];
|
||||
|
||||
Arguments are passed as filters, not as values. The
|
||||
same argument may be referenced multiple times with
|
||||
different inputs (here `f` is run for each element of the
|
||||
input array). Arguments to a function work more like
|
||||
callbacks than like value arguments.
|
||||
|
||||
If you want the value-argument behaviour for defining simple
|
||||
functions, you can just use a variable:
|
||||
|
||||
def addvalue(f): f as $value | map(, + $value);
|
||||
|
||||
With that definition, `addvalue(.foo)` will add the current
|
||||
input's `.foo` field to each element of the array.
|
||||
|
||||
examples:
|
||||
- program: 'def addvalue(f): map(. + [$value]); addvalue(.[0])'
|
||||
input: '[[1,2],[10,20]]'
|
||||
output: ['[[1,2,1], [10,20,10]]']
|
||||
- program: 'def addvalue(f): f as $x | map (. + $x); addvalue(.[0])'
|
||||
input: '[[1,2],[10,20]]'
|
||||
output: ['[[1,2,[1,2]], [10,20,[1,2]]]']
|
||||
|
||||
|
||||
- title: Assignment
|
||||
body: |
|
||||
|
||||
Assignment works a little differently in jq than in most
|
||||
programming languages. jq doesn't distinguish between references
|
||||
to and copies of something - two objects or arrays are either
|
||||
equal or not equal, without any further notion of being "the
|
||||
same object" or "not the same object".
|
||||
|
||||
If an object has two fields which are arrays, `.foo` and `.bar`,
|
||||
and you append something to `.foo`, then `.bar` will not get
|
||||
bigger. Even if you've just set `.bar = .foo`. If you're used to
|
||||
programming in languages like Python, Java, Ruby, Javascript,
|
||||
etc. then you can think of it as though jq does a full deep copy
|
||||
of every object before it does the assignment (for performance,
|
||||
it doesn't actually do that, but that's the general idea).
|
||||
|
||||
entries:
|
||||
- title: "`=`"
|
||||
body: |
|
||||
|
||||
The filter `.foo = 1` will take as input an object
|
||||
and produce as output an object with the "foo" field set to
|
||||
1. There is no notion of "modifying" or "changing" something
|
||||
in jq - all jq values are immutable. For instance,
|
||||
|
||||
.foo = .bar | .foo.baz = 1
|
||||
|
||||
will not have the side-effect of setting .bar.baz to be set
|
||||
to 1, as the similar-looking program in Javascript, Python,
|
||||
Ruby or other languages would. Unlike these languages (but
|
||||
like Haskell and some other functional languages), there is
|
||||
no notion of two arrays or objects being "the same array" or
|
||||
"the same object". They can be equal, or not equal, but if
|
||||
we change one of them in no circumstances will the other
|
||||
change behind our backs.
|
||||
|
||||
This means that it's impossible to build circular values in
|
||||
jq (such as an array whose first element is itself). This is
|
||||
quite intentional, and ensures that anything a jq program
|
||||
can produce can be represented in JSON.
|
||||
|
||||
- title: "`|=`"
|
||||
body: |
|
||||
As well as the assignment operator '=', jq provides the "update"
|
||||
operator '|=', which takes a filter on the right-hand side and
|
||||
works out the new value for the property being assigned to by running
|
||||
the old value through this expression. For instance, .foo |= .+1 will
|
||||
build an object with the "foo" field set to the input's "foo" plus 1.
|
||||
|
||||
This example should show the difference between '=' and '|=':
|
||||
|
||||
Provide input '{"a": {"b": 10}, "b": 20}' to the programs:
|
||||
|
||||
.a = .b
|
||||
.a |= .b
|
||||
|
||||
The former will set the "a" field of the input to the "b" field of the
|
||||
input, and produce the output {"a": 20}. The latter will set the "a"
|
||||
field of the input to the "a" field's "b" field, producing {"a": 10}.
|
||||
|
||||
- title: "`+=`, `-=`, `*=`, `/=`, `//=`"
|
||||
body: |
|
||||
|
||||
jq has a few operators of the form `a op= b`, which are all
|
||||
equivalent to `a |= . op b`. So, `+= 1` can be used to increment values.
|
||||
|
||||
examples:
|
||||
- program: .foo += 1
|
||||
input: '{"foo": 42}'
|
||||
output: ['{"foo": 43}']
|
||||
|
||||
- title: Complex assignments
|
||||
body: |
|
||||
Lots more things are allowed on the left-hand side of a jq assignment
|
||||
than in most langauges. We've already seen simple field accesses on
|
||||
the left hand side, and it's no surprise that array accesses work just
|
||||
as well:
|
||||
|
||||
.posts[0].title = "JQ Manual"
|
||||
|
||||
What may come as a surprise is that the expression on the left may
|
||||
produce multiple results, referring to different points in the input
|
||||
document:
|
||||
|
||||
.posts[].comments |= . + ["this is great"]
|
||||
|
||||
That example appends the string "this is great" to the "comments"
|
||||
array of each post in the input (where the input is an object with a
|
||||
field "posts" which is an array of posts).
|
||||
|
||||
When jq encounters an assignment like 'a = b', it records the "path"
|
||||
taken to select a part of the input document while executing a. This
|
||||
path is then used to find which part of the input to change while
|
||||
executing the assignment. Any filter may be used on the
|
||||
left-hand side of an equals - whichever paths it selects from the
|
||||
input will be where the assignment is performed.
|
||||
|
||||
This is a very powerful operation. Suppose we wanted to add a comment
|
||||
to blog posts, using the same "blog" input above. This time, we only
|
||||
want to comment on the posts written by "stedolan". We can find those
|
||||
posts using the "select" function described earlier:
|
||||
|
||||
.posts[] | select(.author == "stedolan")
|
||||
|
||||
The paths provided by this operation point to each of the posts that
|
||||
"stedolan" wrote, and we can comment on each of them in the same way
|
||||
that we did before:
|
||||
|
||||
(.posts[] | select(.author == "stedolan") | .comments) |= . + ["terrible."]
|
||||
@@ -0,0 +1,27 @@
|
||||
headline: jq
|
||||
|
||||
blurb: |
|
||||
|
||||
jq is a lightweight and flexible command-line JSON processor.
|
||||
|
||||
body1: |
|
||||
|
||||
jq is like `sed` for JSON data - you can use it to slice and filter
|
||||
and map and transform structured data with the same ease that `sed`,
|
||||
`awk`, `grep` and friends let you play with text.
|
||||
|
||||
body2: |
|
||||
|
||||
jq is written in portable C, and it has zero runtime
|
||||
dependencies. You can download a single binary, `scp` it to a far away
|
||||
machine, and expect it to work.
|
||||
|
||||
body3: |
|
||||
|
||||
jq can mangle the data format that you have into the one that you
|
||||
want with very little effort, and the program to do so is often
|
||||
shorter and simpler than you'd expect.
|
||||
|
||||
tail: |
|
||||
Go read the [tutorial](tutorial) for more, or the [manual](manual)
|
||||
for *way* more.
|
||||
@@ -0,0 +1,28 @@
|
||||
DirectoryIndex index.html
|
||||
FileETag All
|
||||
|
||||
# Compress all static assets
|
||||
<IfModule mod_deflate.c>
|
||||
# compress content with type html, text, and css
|
||||
AddOutputFilterByType DEFLATE text/css text/html text/javascript application/javascript application/x-javascript text/js text/plain text/xml
|
||||
|
||||
<IfModule mod_headers.c>
|
||||
# properly handle requests coming from behind proxies
|
||||
Header append Vary User-Agent
|
||||
</IfModule>
|
||||
</IfModule>
|
||||
|
||||
# Cache, aggressively
|
||||
<IfModule mod_expires.c>
|
||||
ExpiresActive On
|
||||
ExpiresDefault "access plus 10 days"
|
||||
|
||||
<FilesMatch "\.(eot|ttf|otf)$">
|
||||
ExpiresDefault "access plus 10 years"
|
||||
</filesMatch>
|
||||
</IfModule>
|
||||
|
||||
# Mime-types
|
||||
AddType application/vnd.ms-fontobject .eot
|
||||
AddType font/ttf .ttf
|
||||
AddType font/otf .otf
|
||||
+1058
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,9 @@
|
||||
/*!
|
||||
* Bootstrap Responsive v2.1.1
|
||||
*
|
||||
* Copyright 2012 Twitter, Inc
|
||||
* Licensed under the Apache License v2.0
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Designed and built with all the love in the world @twitter by @mdo and @fat.
|
||||
*/.clearfix{*zoom:1}.clearfix:before,.clearfix:after{display:table;line-height:0;content:""}.clearfix:after{clear:both}.hide-text{font:0/0 a;color:transparent;text-shadow:none;background-color:transparent;border:0}.input-block-level{display:block;width:100%;min-height:30px;-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box}.hidden{display:none;visibility:hidden}.visible-phone{display:none!important}.visible-tablet{display:none!important}.hidden-desktop{display:none!important}.visible-desktop{display:inherit!important}@media(min-width:768px) and (max-width:979px){.hidden-desktop{display:inherit!important}.visible-desktop{display:none!important}.visible-tablet{display:inherit!important}.hidden-tablet{display:none!important}}@media(max-width:767px){.hidden-desktop{display:inherit!important}.visible-desktop{display:none!important}.visible-phone{display:inherit!important}.hidden-phone{display:none!important}}@media(min-width:1200px){.row{margin-left:-30px;*zoom:1}.row:before,.row:after{display:table;line-height:0;content:""}.row:after{clear:both}[class*="span"]{float:left;min-height:1px;margin-left:30px}.container,.navbar-static-top .container,.navbar-fixed-top .container,.navbar-fixed-bottom .container{width:1170px}.span12{width:1170px}.span11{width:1070px}.span10{width:970px}.span9{width:870px}.span8{width:770px}.span7{width:670px}.span6{width:570px}.span5{width:470px}.span4{width:370px}.span3{width:270px}.span2{width:170px}.span1{width:70px}.offset12{margin-left:1230px}.offset11{margin-left:1130px}.offset10{margin-left:1030px}.offset9{margin-left:930px}.offset8{margin-left:830px}.offset7{margin-left:730px}.offset6{margin-left:630px}.offset5{margin-left:530px}.offset4{margin-left:430px}.offset3{margin-left:330px}.offset2{margin-left:230px}.offset1{margin-left:130px}.row-fluid{width:100%;*zoom:1}.row-fluid:before,.row-fluid:after{display:table;line-height:0;content:""}.row-fluid:after{clear:both}.row-fluid [class*="span"]{display:block;float:left;width:100%;min-height:30px;margin-left:2.564102564102564%;*margin-left:2.5109110747408616%;-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box}.row-fluid [class*="span"]:first-child{margin-left:0}.row-fluid .span12{width:100%;*width:99.94680851063829%}.row-fluid .span11{width:91.45299145299145%;*width:91.39979996362975%}.row-fluid .span10{width:82.90598290598291%;*width:82.8527914166212%}.row-fluid .span9{width:74.35897435897436%;*width:74.30578286961266%}.row-fluid .span8{width:65.81196581196582%;*width:65.75877432260411%}.row-fluid .span7{width:57.26495726495726%;*width:57.21176577559556%}.row-fluid .span6{width:48.717948717948715%;*width:48.664757228587014%}.row-fluid .span5{width:40.17094017094017%;*width:40.11774868157847%}.row-fluid .span4{width:31.623931623931625%;*width:31.570740134569924%}.row-fluid .span3{width:23.076923076923077%;*width:23.023731587561375%}.row-fluid .span2{width:14.52991452991453%;*width:14.476723040552828%}.row-fluid .span1{width:5.982905982905983%;*width:5.929714493544281%}.row-fluid .offset12{margin-left:105.12820512820512%;*margin-left:105.02182214948171%}.row-fluid .offset12:first-child{margin-left:102.56410256410257%;*margin-left:102.45771958537915%}.row-fluid .offset11{margin-left:96.58119658119658%;*margin-left:96.47481360247316%}.row-fluid .offset11:first-child{margin-left:94.01709401709402%;*margin-left:93.91071103837061%}.row-fluid .offset10{margin-left:88.03418803418803%;*margin-left:87.92780505546462%}.row-fluid .offset10:first-child{margin-left:85.47008547008548%;*margin-left:85.36370249136206%}.row-fluid .offset9{margin-left:79.48717948717949%;*margin-left:79.38079650845607%}.row-fluid .offset9:first-child{margin-left:76.92307692307693%;*margin-left:76.81669394435352%}.row-fluid .offset8{margin-left:70.94017094017094%;*margin-left:70.83378796144753%}.row-fluid .offset8:first-child{margin-left:68.37606837606839%;*margin-left:68.26968539734497%}.row-fluid .offset7{margin-left:62.393162393162385%;*margin-left:62.28677941443899%}.row-fluid .offset7:first-child{margin-left:59.82905982905982%;*margin-left:59.72267685033642%}.row-fluid .offset6{margin-left:53.84615384615384%;*margin-left:53.739770867430444%}.row-fluid .offset6:first-child{margin-left:51.28205128205128%;*margin-left:51.175668303327875%}.row-fluid .offset5{margin-left:45.299145299145295%;*margin-left:45.1927623204219%}.row-fluid .offset5:first-child{margin-left:42.73504273504273%;*margin-left:42.62865975631933%}.row-fluid .offset4{margin-left:36.75213675213675%;*margin-left:36.645753773413354%}.row-fluid .offset4:first-child{margin-left:34.18803418803419%;*margin-left:34.081651209310785%}.row-fluid .offset3{margin-left:28.205128205128204%;*margin-left:28.0987452264048%}.row-fluid .offset3:first-child{margin-left:25.641025641025642%;*margin-left:25.53464266230224%}.row-fluid .offset2{margin-left:19.65811965811966%;*margin-left:19.551736679396257%}.row-fluid .offset2:first-child{margin-left:17.094017094017094%;*maLine truncated
|
||||
+5224
File diff suppressed because it is too large.
Load diff
+9
@@ -0,0 +1,9 @@
|
||||
/*!
|
||||
* Bootstrap v2.1.1
|
||||
*
|
||||
* Copyright 2012 Twitter, Inc
|
||||
* Licensed under the Apache License v2.0
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Designed and built with all the love in the world @twitter by @mdo and @fat.
|
||||
*/article,aside,details,figcaption,figure,footer,header,hgroup,nav,section{display:block}audio,canvas,video{display:inline-block;*display:inline;*zoom:1}audio:not([controls]){display:none}html{font-size:100%;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%}a:focus{outline:thin dotted #333;outline:5px auto -webkit-focus-ring-color;outline-offset:-2px}a:hover,a:active{outline:0}sub,sup{position:relative;font-size:75%;line-height:0;vertical-align:baseline}sup{top:-0.5em}sub{bottom:-0.25em}img{width:auto\9;height:auto;max-width:100%;vertical-align:middle;border:0;-ms-interpolation-mode:bicubic}#map_canvas img{max-width:none}button,input,select,textarea{margin:0;font-size:100%;vertical-align:middle}button,input{*overflow:visible;line-height:normal}button::-moz-focus-inner,input::-moz-focus-inner{padding:0;border:0}button,input[type="button"],input[type="reset"],input[type="submit"]{cursor:pointer;-webkit-appearance:button}input[type="search"]{-webkit-box-sizing:content-box;-moz-box-sizing:content-box;box-sizing:content-box;-webkit-appearance:textfield}input[type="search"]::-webkit-search-decoration,input[type="search"]::-webkit-search-cancel-button{-webkit-appearance:none}textarea{overflow:auto;vertical-align:top}.clearfix{*zoom:1}.clearfix:before,.clearfix:after{display:table;line-height:0;content:""}.clearfix:after{clear:both}.hide-text{font:0/0 a;color:transparent;text-shadow:none;background-color:transparent;border:0}.input-block-level{display:block;width:100%;min-height:30px;-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box}body{margin:0;font-family:"Helvetica Neue",Helvetica,Arial,sans-serif;font-size:14px;line-height:20px;color:#333;background-color:#fff}a{color:#08c;text-decoration:none}a:hover{color:#005580;text-decoration:underline}.img-rounded{-webkit-border-radius:6px;-moz-border-radius:6px;border-radius:6px}.img-polaroid{padding:4px;background-color:#fff;border:1px solid #ccc;border:1px solid rgba(0,0,0,0.2);-webkit-box-shadow:0 1px 3px rgba(0,0,0,0.1);-moz-box-shadow:0 1px 3px rgba(0,0,0,0.1);box-shadow:0 1px 3px rgba(0,0,0,0.1)}.img-circle{-webkit-border-radius:500px;-moz-border-radius:500px;border-radius:500px}.row{margin-left:-20px;*zoom:1}.row:before,.row:after{display:table;line-height:0;content:""}.row:after{clear:both}[class*="span"]{float:left;min-height:1px;margin-left:20px}.container,.navbar-static-top .container,.navbar-fixed-top .container,.navbar-fixed-bottom .container{width:940px}.span12{width:940px}.span11{width:860px}.span10{width:780px}.span9{width:700px}.span8{width:620px}.span7{width:540px}.span6{width:460px}.span5{width:380px}.span4{width:300px}.span3{width:220px}.span2{width:140px}.span1{width:60px}.offset12{margin-left:980px}.offset11{margin-left:900px}.offset10{margin-left:820px}.offset9{margin-left:740px}.offset8{margin-left:660px}.offset7{margin-left:580px}.offset6{margin-left:500px}.offset5{margin-left:420px}.offset4{margin-left:340px}.offset3{margin-left:260px}.offset2{margin-left:180px}.offset1{margin-left:100px}.row-fluid{width:100%;*zoom:1}.row-fluid:before,.row-fluid:after{display:table;line-height:0;content:""}.row-fluid:after{clear:both}.row-fluid [class*="span"]{display:block;float:left;width:100%;min-height:30px;margin-left:2.127659574468085%;*margin-left:2.074468085106383%;-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box}.row-fluid [class*="span"]:first-child{margin-left:0}.row-fluid .span12{width:100%;*width:99.94680851063829%}.row-fluid .span11{width:91.48936170212765%;*width:91.43617021276594%}.row-fluid .span10{width:82.97872340425532%;*width:82.92553191489361%}.row-fluid .span9{width:74.46808510638297%;*width:74.41489361702126%}.row-fluid .span8{width:65.95744680851064%;*width:65.90425531914893%}.row-fluid .span7{width:57.44680851063829%;*width:57.39361702127659%}.row-fluid .span6{width:48.93617021276595%;*width:48.88297872340425%}.row-fluid .span5{width:40.42553191489362%;*width:40.37234042553192%}.row-fluid .span4{width:31.914893617021278%;*width:31.861702127659576%}.row-fluid .span3{width:23.404255319148934%;*width:23.351063829787233%}.row-fluid .span2{width:14.893617021276595%;*width:14.840425531914894%}.row-fluid .span1{width:6.382978723404255%;*width:6.329787234042553%}.row-fluid .offset12{margin-left:104.25531914893617%;*margin-left:104.14893617021275%}.row-fluid .offset12:first-child{margin-left:102.12765957446808%;*margin-left:102.02127659574467%}.row-fluid .offset11{margin-left:95.74468085106382%;*margin-left:95.6382978723404%}.row-fluid .offset11:first-child{margin-left:93.61702127659574%;*margin-left:93.51063829787232%}.row-fluid .offset10{margin-left:87.23404255319149%;*margin-left:87.12765957446807%}.row-fluid .offset10:first-child{margin-left:85.1063829787234%;*margin-left:84.99999999999999%}.row-fluid .offset9{margin-left:78.72340425531914%;*margin-left:78.61702127659572%}.row-fluid .offset9:first-child{margin-left:76.59574468085106%;*margin-left:76.48936170212764%}.row-fluiLine truncated
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 8.6 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 12 KiB |
+2027
File diff suppressed because it is too large.
Load diff
+6
@@ -0,0 +1,6 @@
|
||||
/*!
|
||||
* Bootstrap.js by @fat & @mdo
|
||||
* Copyright 2012 Twitter, Inc.
|
||||
* http://www.apache.org/licenses/LICENSE-2.0.txt
|
||||
*/
|
||||
!function(e){e(function(){"use strict";e.support.transition=function(){var e=function(){var e=document.createElement("bootstrap"),t={WebkitTransition:"webkitTransitionEnd",MozTransition:"transitionend",OTransition:"oTransitionEnd otransitionend",transition:"transitionend"},n;for(n in t)if(e.style[n]!==undefined)return t[n]}();return e&&{end:e}}()})}(window.jQuery),!function(e){"use strict";var t='[data-dismiss="alert"]',n=function(n){e(n).on("click",t,this.close)};n.prototype.close=function(t){function s(){i.trigger("closed").remove()}var n=e(this),r=n.attr("data-target"),i;r||(r=n.attr("href"),r=r&&r.replace(/.*(?=#[^\s]*$)/,"")),i=e(r),t&&t.preventDefault(),i.length||(i=n.hasClass("alert")?n:n.parent()),i.trigger(t=e.Event("close"));if(t.isDefaultPrevented())return;i.removeClass("in"),e.support.transition&&i.hasClass("fade")?i.on(e.support.transition.end,s):s()},e.fn.alert=function(t){return this.each(function(){var r=e(this),i=r.data("alert");i||r.data("alert",i=new n(this)),typeof t=="string"&&i[t].call(r)})},e.fn.alert.Constructor=n,e(function(){e("body").on("click.alert.data-api",t,n.prototype.close)})}(window.jQuery),!function(e){"use strict";var t=function(t,n){this.$element=e(t),this.options=e.extend({},e.fn.button.defaults,n)};t.prototype.setState=function(e){var t="disabled",n=this.$element,r=n.data(),i=n.is("input")?"val":"html";e+="Text",r.resetText||n.data("resetText",n[i]()),n[i](r[e]||this.options[e]),setTimeout(function(){e=="loadingText"?n.addClass(t).attr(t,t):n.removeClass(t).removeAttr(t)},0)},t.prototype.toggle=function(){var e=this.$element.closest('[data-toggle="buttons-radio"]');e&&e.find(".active").removeClass("active"),this.$element.toggleClass("active")},e.fn.button=function(n){return this.each(function(){var r=e(this),i=r.data("button"),s=typeof n=="object"&&n;i||r.data("button",i=new t(this,s)),n=="toggle"?i.toggle():n&&i.setState(n)})},e.fn.button.defaults={loadingText:"loading..."},e.fn.button.Constructor=t,e(function(){e("body").on("click.button.data-api","[data-toggle^=button]",function(t){var n=e(t.target);n.hasClass("btn")||(n=n.closest(".btn")),n.button("toggle")})})}(window.jQuery),!function(e){"use strict";var t=function(t,n){this.$element=e(t),this.options=n,this.options.slide&&this.slide(this.options.slide),this.options.pause=="hover"&&this.$element.on("mouseenter",e.proxy(this.pause,this)).on("mouseleave",e.proxy(this.cycle,this))};t.prototype={cycle:function(t){return t||(this.paused=!1),this.options.interval&&!this.paused&&(this.interval=setInterval(e.proxy(this.next,this),this.options.interval)),this},to:function(t){var n=this.$element.find(".item.active"),r=n.parent().children(),i=r.index(n),s=this;if(t>r.length-1||t<0)return;return this.sliding?this.$element.one("slid",function(){s.to(t)}):i==t?this.pause().cycle():this.slide(t>i?"next":"prev",e(r[t]))},pause:function(t){return t||(this.paused=!0),this.$element.find(".next, .prev").length&&e.support.transition.end&&(this.$element.trigger(e.support.transition.end),this.cycle()),clearInterval(this.interval),this.interval=null,this},next:function(){if(this.sliding)return;return this.slide("next")},prev:function(){if(this.sliding)return;return this.slide("prev")},slide:function(t,n){var r=this.$element.find(".item.active"),i=n||r[t](),s=this.interval,o=t=="next"?"left":"right",u=t=="next"?"first":"last",a=this,f=e.Event("slide",{relatedTarget:i[0]});this.sliding=!0,s&&this.pause(),i=i.length?i:this.$element.find(".item")[u]();if(i.hasClass("active"))return;if(e.support.transition&&this.$element.hasClass("slide")){this.$element.trigger(f);if(f.isDefaultPrevented())return;i.addClass(t),i[0].offsetWidth,r.addClass(o),i.addClass(o),this.$element.one(e.support.transition.end,function(){i.removeClass([t,o].join(" ")).addClass("active"),r.removeClass(["active",o].join(" ")),a.sliding=!1,setTimeout(function(){a.$element.trigger("slid")},0)})}else{this.$element.trigger(f);if(f.isDefaultPrevented())return;r.removeClass("active"),i.addClass("active"),this.sliding=!1,this.$element.trigger("slid")}return s&&this.cycle(),this}},e.fn.carousel=function(n){return this.each(function(){var r=e(this),i=r.data("carousel"),s=e.extend({},e.fn.carousel.defaults,typeof n=="object"&&n),o=typeof n=="string"?n:s.slide;i||r.data("carousel",i=new t(this,s)),typeof n=="number"?i.to(n):o?i[o]():s.interval&&i.cycle()})},e.fn.carousel.defaults={interval:5e3,pause:"hover"},e.fn.carousel.Constructor=t,e(function(){e("body").on("click.carousel.data-api","[data-slide]",function(t){var n=e(this),r,i=e(n.attr("data-target")||(r=n.attr("href"))&&r.replace(/.*(?=#[^\s]+$)/,"")),s=!i.data("modal")&&e.extend({},i.data(),n.data());i.carousel(s),t.preventDefault()})})}(window.jQuery),!function(e){"use strict";var t=function(t,n){this.$element=e(t),this.options=e.extend({},e.fn.collapse.defaults,n),this.options.parent&&(this.$parent=e(this.options.parent)),this.options.toggle&&this.toggle()};t.prototype={constructor:t,dimension:function(){var e=this.$element.hLine truncated
|
||||
@@ -0,0 +1,92 @@
|
||||
@charset "utf-8";
|
||||
body {
|
||||
padding-top: 60px;
|
||||
}
|
||||
|
||||
footer {
|
||||
background-color: #F5F5F5;
|
||||
padding: 40px 0;
|
||||
height: 100px;
|
||||
margin-top: 40px;
|
||||
color: #999999;
|
||||
}
|
||||
|
||||
section {
|
||||
padding-top: 30px;
|
||||
}
|
||||
|
||||
.manual-example {
|
||||
border-top: 1px solid #E5E5E5;
|
||||
}
|
||||
|
||||
.manual-example td {
|
||||
white-space: pre-wrap;
|
||||
font-family: Monaco, Menlo, Consolas, "Courier New", monospace;
|
||||
}
|
||||
|
||||
.manual-example td.jqprogram {
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.manual-example th {
|
||||
text-align: right;
|
||||
padding-right: 10px;
|
||||
}
|
||||
|
||||
.tutorial-example {
|
||||
position: relative;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
.tutorial-example pre {
|
||||
margin-bottom: 0px;
|
||||
}
|
||||
|
||||
.tutorial-example a {
|
||||
position: absolute;
|
||||
top: 0px;
|
||||
right: 0px;
|
||||
padding: 15px 8px;
|
||||
color: #777777;
|
||||
font-weight: bold;
|
||||
line-height: 10px;
|
||||
font-size: 12px;
|
||||
border-left: 1px solid #DDDDDD;
|
||||
bottom: 0;
|
||||
display: block;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#blurb {
|
||||
padding-top: 40px;
|
||||
padding-right: 50px;
|
||||
}
|
||||
|
||||
#blurb p {
|
||||
font-size: 20pt;
|
||||
}
|
||||
|
||||
#blurb .btn-group {
|
||||
margin-top: 20px;
|
||||
}
|
||||
|
||||
#multiblurb {
|
||||
line-height: 1.7;
|
||||
text-align: center;
|
||||
font-size: 12pt;
|
||||
}
|
||||
|
||||
#multiblurb code {
|
||||
border: 0;
|
||||
font-size: 12pt;
|
||||
}
|
||||
|
||||
h3 code {
|
||||
border: 0;
|
||||
font-size: 20px;
|
||||
}
|
||||
|
||||
footer {
|
||||
text-align: center;
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 8.0 KiB |
@@ -0,0 +1,2 @@
|
||||
User-agent: *
|
||||
Allow: /
|
||||
@@ -0,0 +1,16 @@
|
||||
# The key value pairs found below are available within the templates.
|
||||
|
||||
|
||||
jq_version: 1.1
|
||||
# Next line is needed for publishing to github pages
|
||||
root: '/jq'
|
||||
# root: ''
|
||||
footer: |
|
||||
|
||||
This website is made with [Bonsai](http://www.tinytree.info) and
|
||||
[Twitter Bootstrap](http://twitter.github.com/bootstrap/), themed
|
||||
with [Bootswatch](http://bootswatch.com).
|
||||
|
||||
jq is licensed under the MIT license (code) and the
|
||||
[CC-BY-3.0](http://creativecommons.org/licenses/by/3.0/) license
|
||||
(docs).
|
||||
Vendored
+34
@@ -0,0 +1,34 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
{% include "shared/head" %}
|
||||
<body id="{{slug}}">
|
||||
{% include "shared/header" %}
|
||||
|
||||
<div class="container">
|
||||
|
||||
<div class="row">
|
||||
<h1>{{headline}}</h1>
|
||||
{% for item in body %}
|
||||
{% if item.text %}
|
||||
{{ item.text | markdownify }}
|
||||
{% endif %}
|
||||
|
||||
{% if item.command %}
|
||||
|
||||
|
||||
{% capture resultID %}{{ "result" | unique}}{% endcapture %}
|
||||
<div class="tutorial-example">
|
||||
<div class="accordion-heading">
|
||||
<pre>{{item.command}}</pre>
|
||||
<a data-toggle="collapse" href="#{{resultID}}">Show result</a>
|
||||
</div>
|
||||
<div id="{{resultID}}" class="accordion-body collapse"><pre>{{item.result}}</pre></div>
|
||||
</div>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
{% include "shared/footer" %}
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Vendored
+41
@@ -0,0 +1,41 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
{% include "shared/head" %}
|
||||
<body id="{{slug}}">
|
||||
{% include "shared/header" %}
|
||||
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<div class="hero-unit">
|
||||
<img src="{{root}}/jq.png" style="width: 400px; height: 220px; float: left; margin-right: 60px">
|
||||
<div id="blurb">
|
||||
{{blurb | markdownify}}
|
||||
<div class="btn-group" style="float:right">
|
||||
<a class="btn btn-primary dropdown-toggle" data-toggle="dropdown" href="#">
|
||||
Download jq-{{jq_version}}
|
||||
<span class="caret"></span>
|
||||
</a>
|
||||
<ul class="dropdown-menu">
|
||||
<li><a href="{{root}}/download/linux_x86_64/jq">Linux (x86_64)</a></li>
|
||||
<li><a href="{{root}}/download/osx_64/jq">OS X (64-bit)</a></li>
|
||||
<li><a href="{{root}}/download">Other platforms and source</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<div style="clear:both;"></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row" id="multiblurb">
|
||||
<div class="span4">{{body1 | markdownify}}</div>
|
||||
<div class="span4">{{body2 | markdownify}}</div>
|
||||
<div class="span4">{{body3 | markdownify}}</div>
|
||||
</div>
|
||||
<div class="row" style="text-align:center; margin-top: 30px">
|
||||
{{tail | markdownify}}
|
||||
</div>
|
||||
</div>
|
||||
{% include "shared/footer" %}
|
||||
</body>
|
||||
</html>
|
||||
Vendored
+110
@@ -0,0 +1,110 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
{% include "shared/head" %}
|
||||
<body id="{{slug}}" data-spy="scroll" data-target="#navcolumn" data-offset="100">
|
||||
{% include "shared/header" %}
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<div class="span3" id="navcolumn">
|
||||
<div class="affix">
|
||||
<h4>Contents</h4>
|
||||
<ul class="nav nav-pills nav-stacked">
|
||||
{% for section in sections %}
|
||||
<li {% if forloop.first %} class="active" {% endif %}>
|
||||
<a href="#{{section.title | sanitize}}">{{section.title}}</a>
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
<form class="form-search">
|
||||
<script type="text/javascript">
|
||||
var section_map = {
|
||||
{% for section in sections %}
|
||||
{% for entry in section.entries %}
|
||||
{{entry.title | json}} : {{entry.title | sanitize | json}},
|
||||
{% endfor %}
|
||||
{{section.title | json}} : {{section.title | sanitize | json}}
|
||||
{% unless forloop.last %},{% endunless %}
|
||||
{% endfor %}
|
||||
};
|
||||
var section_names = $.map(section_map, function(v,k){return k});
|
||||
$(function(){
|
||||
$('#searchbox').typeahead({source: section_names})
|
||||
$('#searchbox').change(function() {
|
||||
if ($(this).val() in section_map) {
|
||||
location.hash = '#' + section_map[$(this).val()];
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
<input type="text"
|
||||
class="input-medium search-query"
|
||||
placeholder="Search"
|
||||
autocomplete="off"
|
||||
id="searchbox">
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
<div class="span9">
|
||||
<h1>{{headline}}</h1>
|
||||
{{ body | markdownify }}
|
||||
{% for section in sections %}
|
||||
<section id="{{section.title | sanitize}}">
|
||||
<h2>{{section.title}}</h2>
|
||||
{{section.body | markdownify}}
|
||||
{% for entry in section.entries %}
|
||||
<section id="{{entry.title | sanitize}}">
|
||||
<h3>
|
||||
{{entry.title | markdownify}}
|
||||
{% if entry.subtitle %}<small>{{entry.subtitle}}</small>{% endif %}
|
||||
</h3>
|
||||
{{entry.body | markdownify}}
|
||||
|
||||
{% if entry.examples %}
|
||||
<div>
|
||||
{% capture exampleID %}{{ "" | unique }}{% endcapture %}
|
||||
<a data-toggle="collapse" href="#example{{exampleID}}">
|
||||
<i class="icon-chevron-right"></i>
|
||||
{% if entry.examples[1] %}Examples{%else%}Example{%endif%}
|
||||
</a>
|
||||
<div id="example{{exampleID}}" class="collapse">
|
||||
|
||||
{% for example in entry.examples %}
|
||||
<table class="manual-example">
|
||||
<tr><th></th><td class="jqprogram">jq '{{example.program}}'</td></tr>
|
||||
<tr><th>Input</th><td>{{example.input}}</td></tr>
|
||||
{% unless example.output[0] %}
|
||||
<tr>
|
||||
<th>Output</th>
|
||||
<td><i>none</i></td>
|
||||
</tr>
|
||||
{% endunless %}
|
||||
{% for output in example.output %}
|
||||
<tr>
|
||||
{% if forloop.index == 1 %}
|
||||
<th>Output</th>
|
||||
{% else %}
|
||||
<th></th>
|
||||
{% endif %}
|
||||
<td>{{output}}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
|
||||
</table>
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
</section>
|
||||
{% endfor %}
|
||||
</section>
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
</div>
|
||||
{% include "shared/footer" %}
|
||||
</body>
|
||||
|
||||
|
||||
</html>
|
||||
Vendored
+5
@@ -0,0 +1,5 @@
|
||||
<footer>
|
||||
<div class="container">
|
||||
{{ footer | markdownify }}
|
||||
</div>
|
||||
</footer>
|
||||
Vendored
+12
@@ -0,0 +1,12 @@
|
||||
<head>
|
||||
<meta charset=UTF-8>
|
||||
<title>{{headline}}</title>
|
||||
<link rel="stylesheet" href="{{root}}/bootstrap/css/bootstrap.css" type="text/css">
|
||||
<link rel="stylesheet" href="{{root}}/css/base.css" type="text/css">
|
||||
<!--[if IE]>
|
||||
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
|
||||
<![endif]-->
|
||||
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
|
||||
<script src="{{root}}/bootstrap/js/bootstrap.js"></script>
|
||||
|
||||
</head>
|
||||
Vendored
+24
@@ -0,0 +1,24 @@
|
||||
|
||||
<div class="navbar navbar-inverse navbar-fixed-top">
|
||||
<div class="navbar-inner">
|
||||
<div class="container">
|
||||
<button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</button>
|
||||
<a class="brand" href="{{root}}/">jq</a>
|
||||
<div class="nav-collapse collapse">
|
||||
<ul class="nav">
|
||||
{% for item in navigation %}
|
||||
<li {% if item.permalink == permalink %} class="active" {% endif %}>
|
||||
<a href="{{root}}{{item.permalink}}">{{item.name}}</a>
|
||||
</li>
|
||||
{% endfor %}
|
||||
<li><a href="https://github.com/stedolan/jq/issues">Issues</a></li>
|
||||
<li><a href="https://github.com/stedolan/jq">Source</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -0,0 +1,525 @@
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "execute.h"
|
||||
|
||||
#include "opcode.h"
|
||||
#include "bytecode.h"
|
||||
#include "compile.h"
|
||||
|
||||
#include "forkable_stack.h"
|
||||
#include "frame_layout.h"
|
||||
|
||||
#include "locfile.h"
|
||||
#include "jv.h"
|
||||
#include "parser.h"
|
||||
#include "builtin.h"
|
||||
|
||||
typedef struct {
|
||||
jv value;
|
||||
int pathidx;
|
||||
} stackval;
|
||||
|
||||
|
||||
jv* pathbuf;
|
||||
int pathsize; // number of allocated elements
|
||||
|
||||
int path_push(stackval sv, jv val) {
|
||||
int pos = sv.pathidx;
|
||||
assert(pos <= pathsize);
|
||||
assert(pos >= 0);
|
||||
if (pos == pathsize) {
|
||||
int oldpathsize = pathsize;
|
||||
pathsize = oldpathsize ? oldpathsize * 2 : 100;
|
||||
pathbuf = realloc(pathbuf, sizeof(pathbuf[0]) * pathsize);
|
||||
for (int i=oldpathsize; i<pathsize; i++) {
|
||||
pathbuf[i] = jv_invalid();
|
||||
}
|
||||
}
|
||||
jv_free(pathbuf[pos]);
|
||||
pathbuf[pos] = val;
|
||||
return pos + 1;
|
||||
}
|
||||
|
||||
stackval stackval_replace(stackval value, jv newjs) {
|
||||
jv_free(value.value);
|
||||
stackval s = {newjs, value.pathidx};
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
// Probably all uses of this function are bugs
|
||||
stackval stackval_root(jv v) {
|
||||
stackval s = {v, 0};
|
||||
return s;
|
||||
}
|
||||
|
||||
struct forkable_stack data_stk;
|
||||
typedef struct {
|
||||
FORKABLE_STACK_HEADER;
|
||||
stackval sv;
|
||||
} data_stk_elem;
|
||||
|
||||
void stack_push(stackval val) {
|
||||
assert(jv_is_valid(val.value));
|
||||
data_stk_elem* s = forkable_stack_push(&data_stk, sizeof(data_stk_elem));
|
||||
s->sv = val;
|
||||
}
|
||||
|
||||
stackval stack_pop() {
|
||||
data_stk_elem* s = forkable_stack_peek(&data_stk);
|
||||
stackval sv = s->sv;
|
||||
if (!forkable_stack_pop_will_free(&data_stk)) {
|
||||
sv.value = jv_copy(sv.value);
|
||||
}
|
||||
forkable_stack_pop(&data_stk);
|
||||
assert(jv_is_valid(sv.value));
|
||||
return sv;
|
||||
}
|
||||
|
||||
struct forkable_stack frame_stk;
|
||||
|
||||
|
||||
struct forkpoint {
|
||||
FORKABLE_STACK_HEADER;
|
||||
struct forkable_stack_state saved_data_stack;
|
||||
struct forkable_stack_state saved_call_stack;
|
||||
};
|
||||
|
||||
struct forkable_stack fork_stk;
|
||||
|
||||
void stack_save(){
|
||||
struct forkpoint* fork = forkable_stack_push(&fork_stk, sizeof(struct forkpoint));
|
||||
forkable_stack_save(&data_stk, &fork->saved_data_stack);
|
||||
forkable_stack_save(&frame_stk, &fork->saved_call_stack);
|
||||
}
|
||||
|
||||
void stack_switch() {
|
||||
struct forkpoint* fork = forkable_stack_peek(&fork_stk);
|
||||
forkable_stack_switch(&data_stk, &fork->saved_data_stack);
|
||||
forkable_stack_switch(&frame_stk, &fork->saved_call_stack);
|
||||
}
|
||||
|
||||
int stack_restore(){
|
||||
while (!forkable_stack_empty(&data_stk) &&
|
||||
forkable_stack_pop_will_free(&data_stk)) {
|
||||
jv_free(stack_pop().value);
|
||||
}
|
||||
while (!forkable_stack_empty(&frame_stk) &&
|
||||
forkable_stack_pop_will_free(&frame_stk)) {
|
||||
frame_pop(&frame_stk);
|
||||
}
|
||||
if (forkable_stack_empty(&fork_stk)) {
|
||||
return 0;
|
||||
} else {
|
||||
struct forkpoint* fork = forkable_stack_peek(&fork_stk);
|
||||
forkable_stack_restore(&data_stk, &fork->saved_data_stack);
|
||||
forkable_stack_restore(&frame_stk, &fork->saved_call_stack);
|
||||
forkable_stack_pop(&fork_stk);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
static struct closure make_closure(struct forkable_stack* stk, frame_ptr fr, uint16_t* pc) {
|
||||
uint16_t level = *pc++;
|
||||
uint16_t idx = *pc++;
|
||||
fr = frame_get_level(stk, fr, level);
|
||||
if (idx & ARG_NEWCLOSURE) {
|
||||
int subfn_idx = idx & ~ARG_NEWCLOSURE;
|
||||
assert(subfn_idx < frame_self(fr)->bc->nsubfunctions);
|
||||
struct closure cl = {frame_self(fr)->bc->subfunctions[subfn_idx],
|
||||
forkable_stack_to_idx(stk, fr)};
|
||||
return cl;
|
||||
} else {
|
||||
return *frame_closure_arg(fr, idx);
|
||||
}
|
||||
}
|
||||
|
||||
void print_error(jv value) {
|
||||
assert(!jv_is_valid(value));
|
||||
jv msg = jv_invalid_get_msg(value);
|
||||
if (jv_get_kind(msg) == JV_KIND_STRING) {
|
||||
fprintf(stderr, "jq: error: %s\n", jv_string_value(msg));
|
||||
}
|
||||
jv_free(msg);
|
||||
}
|
||||
#define ON_BACKTRACK(op) ((op)+NUM_OPCODES)
|
||||
|
||||
jv jq_next() {
|
||||
jv cfunc_input[MAX_CFUNCTION_ARGS];
|
||||
jv cfunc_output[MAX_CFUNCTION_ARGS];
|
||||
|
||||
assert(!forkable_stack_empty(&frame_stk));
|
||||
uint16_t* pc = *frame_current_retaddr(&frame_stk);
|
||||
frame_pop(&frame_stk);
|
||||
|
||||
assert(!forkable_stack_empty(&frame_stk));
|
||||
|
||||
int backtracking = 0;
|
||||
while (1) {
|
||||
uint16_t opcode = *pc;
|
||||
|
||||
#if JQ_DEBUG
|
||||
dump_operation(frame_current_bytecode(&frame_stk), pc);
|
||||
printf("\t");
|
||||
const struct opcode_description* opdesc = opcode_describe(opcode);
|
||||
data_stk_elem* param;
|
||||
for (int i=0; i<opdesc->stack_in; i++) {
|
||||
if (i == 0) {
|
||||
param = forkable_stack_peek(&data_stk);
|
||||
} else {
|
||||
printf(" | ");
|
||||
param = forkable_stack_peek_next(&data_stk, param);
|
||||
}
|
||||
if (!param) break;
|
||||
jv_dump(jv_copy(param->sv.value), 0);
|
||||
printf("<%d>", jv_get_refcnt(param->sv.value));
|
||||
}
|
||||
|
||||
if (backtracking) printf("\t<backtracking>");
|
||||
|
||||
printf("\n");
|
||||
#endif
|
||||
if (backtracking) {
|
||||
opcode = ON_BACKTRACK(opcode);
|
||||
backtracking = 0;
|
||||
}
|
||||
pc++;
|
||||
|
||||
switch (opcode) {
|
||||
default: assert(0 && "invalid instruction");
|
||||
|
||||
case LOADK: {
|
||||
jv v = jv_array_get(jv_copy(frame_current_bytecode(&frame_stk)->constants), *pc++);
|
||||
assert(jv_is_valid(v));
|
||||
stack_push(stackval_replace(stack_pop(), v));
|
||||
break;
|
||||
}
|
||||
|
||||
case DUP: {
|
||||
stackval v = stack_pop();
|
||||
stackval v2 = v;
|
||||
v2.value = jv_copy(v.value);
|
||||
stack_push(v);
|
||||
stack_push(v2);
|
||||
break;
|
||||
}
|
||||
|
||||
case SWAP: {
|
||||
stackval a = stack_pop();
|
||||
stackval b = stack_pop();
|
||||
stack_push(a);
|
||||
stack_push(b);
|
||||
break;
|
||||
}
|
||||
|
||||
case POP: {
|
||||
jv_free(stack_pop().value);
|
||||
break;
|
||||
}
|
||||
|
||||
case APPEND: {
|
||||
// FIXME paths
|
||||
jv v = stack_pop().value;
|
||||
jv array = stack_pop().value;
|
||||
array = jv_array_append(array, v);
|
||||
stack_push(stackval_root(array));
|
||||
break;
|
||||
}
|
||||
|
||||
case INSERT: {
|
||||
stackval stktop = stack_pop();
|
||||
jv v = stack_pop().value;
|
||||
jv k = stack_pop().value;
|
||||
stackval objv = stack_pop();
|
||||
assert(jv_get_kind(k) == JV_KIND_STRING);
|
||||
assert(jv_get_kind(objv.value) == JV_KIND_OBJECT);
|
||||
objv.value = jv_object_set(objv.value, k, v);
|
||||
stack_push(objv);
|
||||
stack_push(stktop);
|
||||
break;
|
||||
}
|
||||
|
||||
// FIXME: loadv/storev may do too much copying/freeing
|
||||
case LOADV: {
|
||||
uint16_t level = *pc++;
|
||||
uint16_t v = *pc++;
|
||||
frame_ptr fp = frame_get_level(&frame_stk, frame_current(&frame_stk), level);
|
||||
jv* var = frame_local_var(fp, v);
|
||||
#if JQ_DEBUG
|
||||
printf("V%d = ", v);
|
||||
jv_dump(jv_copy(*var), 0);
|
||||
printf("\n");
|
||||
#endif
|
||||
stack_push(stackval_replace(stack_pop(), jv_copy(*var)));
|
||||
break;
|
||||
}
|
||||
|
||||
case STOREV: {
|
||||
uint16_t level = *pc++;
|
||||
uint16_t v = *pc++;
|
||||
frame_ptr fp = frame_get_level(&frame_stk, frame_current(&frame_stk), level);
|
||||
jv* var = frame_local_var(fp, v);
|
||||
stackval val = stack_pop();
|
||||
#if JQ_DEBUG
|
||||
printf("V%d = ", v);
|
||||
jv_dump(jv_copy(val.value), 0);
|
||||
printf("\n");
|
||||
#endif
|
||||
jv_free(*var);
|
||||
*var = val.value;
|
||||
break;
|
||||
}
|
||||
|
||||
case ASSIGN: {
|
||||
stackval replacement = stack_pop();
|
||||
stackval path_end = stack_pop();
|
||||
stackval path_start = stack_pop();
|
||||
jv_free(path_end.value);
|
||||
jv_free(path_start.value);
|
||||
|
||||
uint16_t level = *pc++;
|
||||
uint16_t v = *pc++;
|
||||
frame_ptr fp = frame_get_level(&frame_stk, frame_current(&frame_stk), level);
|
||||
jv* var = frame_local_var(fp, v);
|
||||
jv result = jv_insert(*var, replacement.value, pathbuf + path_start.pathidx, path_end.pathidx - path_start.pathidx);
|
||||
if (jv_is_valid(result)) {
|
||||
*var = result;
|
||||
} else {
|
||||
print_error(result);
|
||||
*var = jv_null();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case INDEX: {
|
||||
stackval t = stack_pop();
|
||||
jv k = stack_pop().value;
|
||||
int pathidx = path_push(t, jv_copy(k));
|
||||
jv v = jv_lookup(t.value, k);
|
||||
if (jv_is_valid(v)) {
|
||||
stackval sv;
|
||||
sv.value = v;
|
||||
sv.pathidx = pathidx;
|
||||
stack_push(sv);
|
||||
} else {
|
||||
print_error(v);
|
||||
goto do_backtrack;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
case JUMP: {
|
||||
uint16_t offset = *pc++;
|
||||
pc += offset;
|
||||
break;
|
||||
}
|
||||
|
||||
case JUMP_F: {
|
||||
uint16_t offset = *pc++;
|
||||
stackval t = stack_pop();
|
||||
jv_kind kind = jv_get_kind(t.value);
|
||||
if (kind == JV_KIND_FALSE || kind == JV_KIND_NULL) {
|
||||
pc += offset;
|
||||
}
|
||||
stack_push(t); // FIXME do this better
|
||||
break;
|
||||
}
|
||||
|
||||
case EACH:
|
||||
stack_push(stackval_root(jv_number(-1)));
|
||||
// fallthrough
|
||||
case ON_BACKTRACK(EACH): {
|
||||
int idx = jv_number_value(stack_pop().value);
|
||||
stackval container = stack_pop();
|
||||
|
||||
int keep_going;
|
||||
jv key, value;
|
||||
if (jv_get_kind(container.value) == JV_KIND_ARRAY) {
|
||||
if (opcode == EACH) idx = 0;
|
||||
else idx = idx + 1;
|
||||
keep_going = idx < jv_array_length(jv_copy(container.value));
|
||||
if (keep_going) {
|
||||
key = jv_number(idx);
|
||||
value = jv_array_get(jv_copy(container.value), idx);
|
||||
}
|
||||
} else if (jv_get_kind(container.value) == JV_KIND_OBJECT) {
|
||||
if (opcode == EACH) idx = jv_object_iter(container.value);
|
||||
else idx = jv_object_iter_next(container.value, idx);
|
||||
keep_going = jv_object_iter_valid(container.value, idx);
|
||||
if (keep_going) {
|
||||
key = jv_object_iter_key(container.value, idx);
|
||||
value = jv_object_iter_value(container.value, idx);
|
||||
}
|
||||
} else {
|
||||
assert(opcode == EACH);
|
||||
print_error(jv_invalid_with_msg(jv_string_fmt("Cannot iterate over %s",
|
||||
jv_kind_name(jv_get_kind(container.value)))));
|
||||
keep_going = 0;
|
||||
}
|
||||
|
||||
if (!keep_going) {
|
||||
jv_free(container.value);
|
||||
goto do_backtrack;
|
||||
} else {
|
||||
stack_save();
|
||||
stack_push(container);
|
||||
stack_push(stackval_root(jv_number(idx)));
|
||||
frame_push_backtrack(&frame_stk, pc - 1);
|
||||
stack_switch();
|
||||
|
||||
stackval sv = {value,
|
||||
path_push(container, key)};
|
||||
stack_push(sv);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
do_backtrack:
|
||||
case BACKTRACK: {
|
||||
if (!stack_restore()) {
|
||||
return jv_invalid();
|
||||
}
|
||||
pc = *frame_current_retaddr(&frame_stk);
|
||||
frame_pop(&frame_stk);
|
||||
backtracking = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
case FORK: {
|
||||
stack_save();
|
||||
frame_push_backtrack(&frame_stk, pc - 1);
|
||||
stack_switch();
|
||||
pc++; // skip offset this time
|
||||
break;
|
||||
}
|
||||
|
||||
case ON_BACKTRACK(FORK): {
|
||||
uint16_t offset = *pc++;
|
||||
pc += offset;
|
||||
break;
|
||||
}
|
||||
|
||||
case YIELD: {
|
||||
jv value = stack_pop().value;
|
||||
frame_push_backtrack(&frame_stk, pc);
|
||||
return value;
|
||||
}
|
||||
|
||||
case CALL_BUILTIN_1_1: {
|
||||
assert(*pc == 1); // no closure args allowed
|
||||
pc++; // skip nclosures
|
||||
pc++; // skip level
|
||||
stackval top = stack_pop();
|
||||
cfunc_input[0] = top.value;
|
||||
struct cfunction* func = &frame_current_bytecode(&frame_stk)->globals->cfunctions[*pc++];
|
||||
func->fptr(cfunc_input, cfunc_output);
|
||||
top.value = cfunc_output[0];
|
||||
if (jv_is_valid(top.value)) {
|
||||
stack_push(top);
|
||||
} else {
|
||||
print_error(top.value);
|
||||
goto do_backtrack;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case CALL_BUILTIN_3_1: {
|
||||
assert(*pc == 1); // no closure args allowed
|
||||
pc++; // skip nclosures
|
||||
pc++; // skip level
|
||||
stackval top = stack_pop();
|
||||
jv a = stack_pop().value;
|
||||
jv b = stack_pop().value;
|
||||
cfunc_input[0] = top.value;
|
||||
cfunc_input[1] = a;
|
||||
cfunc_input[2] = b;
|
||||
struct cfunction* func = &frame_current_bytecode(&frame_stk)->globals->cfunctions[*pc++];
|
||||
func->fptr(cfunc_input, cfunc_output);
|
||||
top.value = cfunc_output[0];
|
||||
if (jv_is_valid(top.value)) {
|
||||
stack_push(top);
|
||||
} else {
|
||||
print_error(top.value);
|
||||
goto do_backtrack;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case CALL_1_1: {
|
||||
uint16_t nclosures = *pc++;
|
||||
frame_ptr new_frame = frame_push(&frame_stk,
|
||||
make_closure(&frame_stk, frame_current(&frame_stk), pc),
|
||||
pc + nclosures * 2);
|
||||
pc += 2;
|
||||
frame_ptr old_frame = forkable_stack_peek_next(&frame_stk, new_frame);
|
||||
assert(nclosures - 1 == frame_self(new_frame)->bc->nclosures);
|
||||
for (int i=0; i<nclosures-1; i++) {
|
||||
*frame_closure_arg(new_frame, i) = make_closure(&frame_stk, old_frame, pc);
|
||||
pc += 2;
|
||||
}
|
||||
|
||||
pc = frame_current_bytecode(&frame_stk)->code;
|
||||
break;
|
||||
}
|
||||
|
||||
case RET: {
|
||||
pc = *frame_current_retaddr(&frame_stk);
|
||||
frame_pop(&frame_stk);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void jq_init(struct bytecode* bc, jv input) {
|
||||
forkable_stack_init(&data_stk, sizeof(stackval) * 1000); // FIXME: lower this number, see if it breaks
|
||||
forkable_stack_init(&frame_stk, 10240); // FIXME: lower this number, see if it breaks
|
||||
forkable_stack_init(&fork_stk, 10240); // FIXME: lower this number, see if it breaks
|
||||
|
||||
stack_push(stackval_root(input));
|
||||
struct closure top = {bc, -1};
|
||||
frame_push(&frame_stk, top, 0);
|
||||
frame_push_backtrack(&frame_stk, bc->code);
|
||||
}
|
||||
|
||||
void jq_teardown() {
|
||||
while (stack_restore()) {}
|
||||
|
||||
assert(forkable_stack_empty(&fork_stk));
|
||||
assert(forkable_stack_empty(&data_stk));
|
||||
assert(forkable_stack_empty(&frame_stk));
|
||||
forkable_stack_free(&fork_stk);
|
||||
forkable_stack_free(&data_stk);
|
||||
forkable_stack_free(&frame_stk);
|
||||
|
||||
for (int i=0; i<pathsize; i++) {
|
||||
jv_free(pathbuf[i]);
|
||||
}
|
||||
free(pathbuf);
|
||||
pathbuf = 0;
|
||||
pathsize = 0;
|
||||
}
|
||||
|
||||
struct bytecode* jq_compile(const char* str) {
|
||||
struct locfile locations;
|
||||
locfile_init(&locations, str, strlen(str));
|
||||
block program;
|
||||
struct bytecode* bc = 0;
|
||||
int nerrors = jq_parse(&locations, &program);
|
||||
if (nerrors == 0) {
|
||||
block_append(&program, block_join(gen_op_simple(YIELD), gen_op_simple(BACKTRACK)));
|
||||
program = builtins_bind(program);
|
||||
nerrors = block_compile(program, &locations, &bc);
|
||||
block_free(program);
|
||||
}
|
||||
if (nerrors) {
|
||||
fprintf(stderr, "%d compile %s\n", nerrors, nerrors > 1 ? "errors" : "error");
|
||||
}
|
||||
locfile_free(&locations);
|
||||
return bc;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
#ifndef EXECUTE_H
|
||||
#define EXECUTE_H
|
||||
#include "bytecode.h"
|
||||
|
||||
|
||||
struct bytecode* jq_compile(const char* str);
|
||||
|
||||
void jq_init(struct bytecode* bc, jv value);
|
||||
jv jq_next();
|
||||
void jq_teardown();
|
||||
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,162 @@
|
||||
#ifndef FORKABLE_STACK_H
|
||||
#define FORKABLE_STACK_H
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
struct forkable_stack_header {
|
||||
/* distance in bytes between this header and the header
|
||||
of the next object on the stack */
|
||||
int next_delta;
|
||||
};
|
||||
|
||||
#define FORKABLE_STACK_HEADER struct forkable_stack_header fk_header_
|
||||
|
||||
struct forkable_stack {
|
||||
// Stack grows down from stk+length
|
||||
|
||||
char* stk;
|
||||
|
||||
// stk+length is just past end of allocated area
|
||||
// stk = malloc(length)
|
||||
int length;
|
||||
|
||||
// stk+pos is header of top object, or stk+length if empty
|
||||
int pos;
|
||||
|
||||
// everything after-or-including stk+savedlimit must be preserved
|
||||
int savedlimit;
|
||||
};
|
||||
|
||||
static void forkable_stack_check(struct forkable_stack* s) {
|
||||
assert(s->stk);
|
||||
assert(s->length > 0);
|
||||
assert(s->pos >= 0 && s->pos <= s->length);
|
||||
assert(s->savedlimit >= 0 && s->savedlimit <= s->length);
|
||||
}
|
||||
|
||||
static int forkable_stack_empty(struct forkable_stack* s) {
|
||||
return s->pos == s->length;
|
||||
}
|
||||
|
||||
static void forkable_stack_init(struct forkable_stack* s, size_t sz) {
|
||||
s->stk = malloc(sz);
|
||||
s->length = sz;
|
||||
s->pos = s->length;
|
||||
s->savedlimit = s->length;
|
||||
forkable_stack_check(s);
|
||||
}
|
||||
|
||||
static void forkable_stack_free(struct forkable_stack* s) {
|
||||
assert(forkable_stack_empty(s));
|
||||
assert(s->savedlimit == s->length);
|
||||
free(s->stk);
|
||||
s->stk = 0;
|
||||
}
|
||||
|
||||
static void* forkable_stack_push(struct forkable_stack* s, size_t sz_size) {
|
||||
assert(sz_size > 0 && sz_size % sizeof(struct forkable_stack_header) == 0);
|
||||
int size = (int)sz_size;
|
||||
forkable_stack_check(s);
|
||||
int curr = s->pos < s->savedlimit ? s->pos : s->savedlimit;
|
||||
if (curr - size < 0) {
|
||||
//assert(0);
|
||||
int oldlen = s->length;
|
||||
s->length = (size + oldlen + 1024) * 2;
|
||||
s->stk = realloc(s->stk, s->length);
|
||||
int shift = s->length - oldlen;
|
||||
memmove(s->stk + shift, s->stk, oldlen);
|
||||
s->pos += shift;
|
||||
s->savedlimit += shift;
|
||||
curr += shift;
|
||||
}
|
||||
int newpos = curr - size;
|
||||
assert(newpos < s->pos);
|
||||
void* ret = (void*)(s->stk + newpos);
|
||||
((struct forkable_stack_header*)ret)->next_delta = s->pos - newpos;
|
||||
s->pos = newpos;
|
||||
forkable_stack_check(s);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void* forkable_stack_peek(struct forkable_stack* s) {
|
||||
assert(!forkable_stack_empty(s));
|
||||
forkable_stack_check(s);
|
||||
return (void*)(s->stk + s->pos);
|
||||
}
|
||||
|
||||
static void* forkable_stack_peek_next(struct forkable_stack* s, void* top) {
|
||||
forkable_stack_check(s);
|
||||
struct forkable_stack_header* elem = top;
|
||||
int elempos = (char*)top - s->stk;
|
||||
assert(elempos >= 0 && elempos < s->length);
|
||||
assert(elempos + elem->next_delta <= s->length);
|
||||
if (elempos + elem->next_delta < s->length) {
|
||||
return (void*)(s->stk + elempos + elem->next_delta);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Returns 1 if the next forkable_stack_pop will permanently remove an
|
||||
// object from the stack (i.e. the top object was not saved with a fork)
|
||||
static int forkable_stack_pop_will_free(struct forkable_stack* s) {
|
||||
return s->pos < s->savedlimit ? 1 : 0;
|
||||
}
|
||||
|
||||
static void forkable_stack_pop(struct forkable_stack* s) {
|
||||
forkable_stack_check(s);
|
||||
struct forkable_stack_header* elem = forkable_stack_peek(s);
|
||||
s->pos += elem->next_delta;
|
||||
}
|
||||
|
||||
|
||||
|
||||
struct forkable_stack_state {
|
||||
int prevpos, prevlimit;
|
||||
};
|
||||
|
||||
static void forkable_stack_save(struct forkable_stack* s, struct forkable_stack_state* state) {
|
||||
forkable_stack_check(s);
|
||||
state->prevpos = s->pos;
|
||||
state->prevlimit = s->savedlimit;
|
||||
if (s->pos < s->savedlimit) s->savedlimit = s->pos;
|
||||
}
|
||||
|
||||
static void forkable_stack_switch(struct forkable_stack* s, struct forkable_stack_state* state) {
|
||||
forkable_stack_check(s);
|
||||
int curr_pos = s->pos;
|
||||
s->pos = state->prevpos;
|
||||
state->prevpos = curr_pos;
|
||||
|
||||
int curr_limit = s->savedlimit;
|
||||
if (curr_pos < curr_limit) s->savedlimit = curr_pos;
|
||||
//state->prevlimit = curr_limit; FIXME clean up
|
||||
forkable_stack_check(s);
|
||||
}
|
||||
|
||||
static void forkable_stack_restore(struct forkable_stack* s, struct forkable_stack_state* state) {
|
||||
forkable_stack_check(s);
|
||||
assert(s->savedlimit <= state->prevpos);
|
||||
assert(s->savedlimit <= state->prevlimit);
|
||||
s->pos = state->prevpos;
|
||||
s->savedlimit = state->prevlimit;
|
||||
forkable_stack_check(s);
|
||||
}
|
||||
|
||||
typedef int stack_idx;
|
||||
|
||||
static stack_idx forkable_stack_to_idx(struct forkable_stack* s, void* ptr) {
|
||||
char* item = ptr;
|
||||
int pos = item - s->stk;
|
||||
assert(pos >= 0 && pos < s->length);
|
||||
return s->length - pos;
|
||||
}
|
||||
|
||||
static void* forkable_stack_from_idx(struct forkable_stack* s, stack_idx idx) {
|
||||
assert(idx >= 1 && idx <= s->length);
|
||||
return &s->stk[s->length - idx];
|
||||
}
|
||||
|
||||
#endif
|
||||
+118
@@ -0,0 +1,118 @@
|
||||
#ifndef FRAME_LAYOUT_H
|
||||
#include "forkable_stack.h"
|
||||
#include "bytecode.h"
|
||||
|
||||
struct closure {
|
||||
struct bytecode* bc;
|
||||
stack_idx env;
|
||||
};
|
||||
|
||||
struct continuation {
|
||||
struct bytecode* bc;
|
||||
stack_idx env;
|
||||
uint16_t* retaddr;
|
||||
// FIXME: probably not necessary as a separate field
|
||||
int is_backtrack_frame;
|
||||
};
|
||||
|
||||
typedef union frame_elem {
|
||||
FORKABLE_STACK_HEADER;
|
||||
struct continuation cont;
|
||||
struct closure closure;
|
||||
jv jsonval;
|
||||
} *frame_ptr;
|
||||
|
||||
/*
|
||||
* Frame layout
|
||||
* fr[0] - FORKABLE_STACK_HEADER (next pointer)
|
||||
* fr[1] - self (used to store return addresses, etc)
|
||||
* fr[2...nclosures+2] - closure params
|
||||
* fr[nclosures+2..nclosures+nlocals+2] - local variables
|
||||
*/
|
||||
|
||||
static int frame_size(struct bytecode* bc) {
|
||||
return sizeof(union frame_elem) * (bc->nclosures + bc->nlocals + 2);
|
||||
}
|
||||
|
||||
static struct continuation* frame_self(frame_ptr fr) {
|
||||
return &fr[1].cont;
|
||||
}
|
||||
|
||||
static struct closure* frame_closure_arg(frame_ptr fr, int closure) {
|
||||
assert(closure >= 0);
|
||||
assert(closure < frame_self(fr)->bc->nclosures);
|
||||
return &fr[2+closure].closure;
|
||||
}
|
||||
|
||||
static jv* frame_local_var(frame_ptr fr, int var) {
|
||||
assert(var >= 0);
|
||||
assert(var < frame_self(fr)->bc->nlocals);
|
||||
return &fr[2 + frame_self(fr)->bc->nclosures + var].jsonval;
|
||||
}
|
||||
|
||||
|
||||
static frame_ptr frame_current(struct forkable_stack* stk) {
|
||||
frame_ptr fp = forkable_stack_peek(stk);
|
||||
if (forkable_stack_peek_next(stk, fp)) {
|
||||
struct bytecode* bc = frame_self(forkable_stack_peek_next(stk, fp))->bc;
|
||||
assert(frame_self(fp)->retaddr >= bc->code && frame_self(fp)->retaddr < bc->code + bc->codelen);
|
||||
} else {
|
||||
assert(frame_self(fp)->retaddr == 0);
|
||||
}
|
||||
return fp;
|
||||
}
|
||||
|
||||
static struct bytecode* frame_current_bytecode(struct forkable_stack* stk) {
|
||||
return frame_self(frame_current(stk))->bc;
|
||||
}
|
||||
static uint16_t** frame_current_retaddr(struct forkable_stack* stk) {
|
||||
return &frame_self(frame_current(stk))->retaddr;
|
||||
}
|
||||
|
||||
static frame_ptr frame_get_parent(struct forkable_stack* stk, frame_ptr fr) {
|
||||
return forkable_stack_from_idx(stk, frame_self(fr)->env);
|
||||
}
|
||||
|
||||
static frame_ptr frame_get_level(struct forkable_stack* stk, frame_ptr fr, int level) {
|
||||
for (int i=0; i<level; i++) {
|
||||
fr = frame_get_parent(stk, fr);
|
||||
}
|
||||
return fr;
|
||||
}
|
||||
|
||||
static frame_ptr frame_push(struct forkable_stack* stk, struct closure cl, uint16_t* retaddr) {
|
||||
frame_ptr fp = forkable_stack_push(stk, frame_size(cl.bc));
|
||||
struct continuation* cc = frame_self(fp);
|
||||
cc->bc = cl.bc;
|
||||
cc->env = cl.env;
|
||||
cc->retaddr = retaddr;
|
||||
cc->is_backtrack_frame = 0;
|
||||
for (int i=0; i<cl.bc->nlocals; i++) {
|
||||
*frame_local_var(fp, i) = jv_invalid();
|
||||
}
|
||||
return fp;
|
||||
}
|
||||
|
||||
static frame_ptr frame_push_backtrack(struct forkable_stack* stk, uint16_t* retaddr) {
|
||||
struct continuation cc = *frame_self(frame_current(stk));
|
||||
frame_ptr fp = forkable_stack_push(stk, sizeof(union frame_elem) * 2);
|
||||
assert(!cc.is_backtrack_frame);
|
||||
cc.is_backtrack_frame = 1;
|
||||
cc.retaddr = retaddr;
|
||||
*frame_self(fp) = cc;
|
||||
return fp;
|
||||
}
|
||||
|
||||
static void frame_pop(struct forkable_stack* stk) {
|
||||
frame_ptr fp = frame_current(stk);
|
||||
if (forkable_stack_pop_will_free(stk) &&
|
||||
!frame_self(fp)->is_backtrack_frame) {
|
||||
int nlocals = frame_self(fp)->bc->nlocals;
|
||||
for (int i=0; i<nlocals; i++) {
|
||||
jv_free(*frame_local_var(fp, i));
|
||||
}
|
||||
}
|
||||
forkable_stack_pop(stk);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,30 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
mask = lambda n: (1 << n) - 1
|
||||
|
||||
|
||||
def print_table(type, name, t):
|
||||
assert len(t) == 256
|
||||
print "static const",type, name+"[]", "="
|
||||
first = True
|
||||
for i in range(0,len(t),16):
|
||||
print (" {" if i == 0 else " ") +\
|
||||
", ".join("0x%02x"%n for n in t[i:i+16]) + \
|
||||
("," if i + 16 < 256 else "};")
|
||||
|
||||
|
||||
def utf8info(c):
|
||||
if c < 0x80: return 1, mask(7)
|
||||
if 0x80 <= c <= 0xBF: return 255, mask(6)
|
||||
if 0xC0 <= c <= 0xC1: return 0, 0
|
||||
if 0xC2 <= c <= 0xDF: return 2, mask(5)
|
||||
if 0xE0 <= c <= 0xEF: return 3, mask(4)
|
||||
if 0xF0 <= c <= 0xF4: return 4, mask(3)
|
||||
if 0xF4 <= c <= 0xFF: return 0, 0
|
||||
|
||||
table = lambda i: [utf8info(c)[i] for c in range(256)]
|
||||
|
||||
print "#define UTF8_CONTINUATION_BYTE ((unsigned char)255)"
|
||||
|
||||
print_table("unsigned char", "utf8_coding_length", table(0))
|
||||
print_table("unsigned char", "utf8_coding_bits", table(1))
|
||||
@@ -0,0 +1,230 @@
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "jv.h"
|
||||
#include "execute.h"
|
||||
|
||||
static void jv_test();
|
||||
static void run_jq_tests();
|
||||
|
||||
int main() {
|
||||
jv_test();
|
||||
run_jq_tests();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
static int skipline(const char* buf) {
|
||||
int p = 0;
|
||||
while (buf[p] == ' ' || buf[p] == '\t') p++;
|
||||
if (buf[p] == '#' || buf[p] == '\n' || buf[p] == 0) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void run_jq_tests() {
|
||||
FILE* testdata = fopen("testdata","r");
|
||||
char buf[4096];
|
||||
int tests = 0, passed = 0;
|
||||
|
||||
while (1) {
|
||||
if (!fgets(buf, sizeof(buf), testdata)) break;
|
||||
if (skipline(buf)) continue;
|
||||
printf("Testing %s\n", buf);
|
||||
int pass = 1;
|
||||
struct bytecode* bc = jq_compile(buf);
|
||||
assert(bc);
|
||||
printf("Disassembly:\n");
|
||||
dump_disassembly(2, bc);
|
||||
printf("\n");
|
||||
fgets(buf, sizeof(buf), testdata);
|
||||
jv input = jv_parse(buf);
|
||||
assert(jv_is_valid(input));
|
||||
jq_init(bc, input);
|
||||
|
||||
while (fgets(buf, sizeof(buf), testdata)) {
|
||||
if (skipline(buf)) break;
|
||||
jv expected = jv_parse(buf);
|
||||
assert(jv_is_valid(expected));
|
||||
jv actual = jq_next();
|
||||
if (!jv_is_valid(actual)) {
|
||||
jv_free(actual);
|
||||
printf("Insufficient results\n");
|
||||
pass = 0;
|
||||
break;
|
||||
} else if (!jv_equal(jv_copy(expected), jv_copy(actual))) {
|
||||
printf("Expected ");
|
||||
jv_dump(jv_copy(expected), 0);
|
||||
printf(", but got ");
|
||||
jv_dump(jv_copy(actual), 0);
|
||||
printf("\n");
|
||||
pass = 0;
|
||||
}
|
||||
jv as_string = jv_dump_string(jv_copy(expected), rand());
|
||||
jv reparsed = jv_parse_sized(jv_string_value(as_string), jv_string_length(jv_copy(as_string)));
|
||||
assert(jv_equal(jv_copy(expected), jv_copy(reparsed)));
|
||||
jv_free(as_string);
|
||||
jv_free(reparsed);
|
||||
jv_free(expected);
|
||||
jv_free(actual);
|
||||
}
|
||||
if (pass) {
|
||||
jv extra = jq_next();
|
||||
if (jv_is_valid(extra)) {
|
||||
printf("Superfluous result: ");
|
||||
jv_dump(extra, 0);
|
||||
printf("\n");
|
||||
pass = 0;
|
||||
} else {
|
||||
jv_free(extra);
|
||||
}
|
||||
}
|
||||
jq_teardown();
|
||||
bytecode_free(bc);
|
||||
tests++;
|
||||
passed+=pass;
|
||||
}
|
||||
fclose(testdata);
|
||||
printf("%d of %d tests passed\n", passed,tests);
|
||||
if (passed != tests) exit(1);
|
||||
}
|
||||
|
||||
|
||||
static void jv_test() {
|
||||
/// Arrays and numbers
|
||||
{
|
||||
jv a = jv_array();
|
||||
assert(jv_get_kind(a) == JV_KIND_ARRAY);
|
||||
assert(jv_array_length(jv_copy(a)) == 0);
|
||||
assert(jv_array_length(jv_copy(a)) == 0);
|
||||
|
||||
a = jv_array_append(a, jv_number(42));
|
||||
assert(jv_array_length(jv_copy(a)) == 1);
|
||||
assert(jv_number_value(jv_array_get(jv_copy(a), 0)) == 42);
|
||||
|
||||
jv a2 = jv_array_append(jv_array(), jv_number(42));
|
||||
assert(jv_equal(jv_copy(a), jv_copy(a)));
|
||||
assert(jv_equal(jv_copy(a2), jv_copy(a2)));
|
||||
assert(jv_equal(jv_copy(a), jv_copy(a2)));
|
||||
assert(jv_equal(jv_copy(a2), jv_copy(a)));
|
||||
jv_free(a2);
|
||||
|
||||
a2 = jv_array_append(jv_array(), jv_number(19));
|
||||
assert(!jv_equal(jv_copy(a), jv_copy(a2)));
|
||||
assert(!jv_equal(jv_copy(a2), jv_copy(a)));
|
||||
jv_free(a2);
|
||||
|
||||
|
||||
assert(a.val.complex.ptr->count == 1);
|
||||
a = jv_array_append(a, jv_copy(a));
|
||||
assert(a.val.complex.ptr->count == 1);
|
||||
|
||||
assert(jv_array_length(jv_copy(a)) == 2);
|
||||
assert(jv_number_value(jv_array_get(jv_copy(a), 0)) == 42);
|
||||
|
||||
for (int i=0; i<10; i++) {
|
||||
jv subarray = jv_array_get(jv_copy(a), 1);
|
||||
assert(jv_get_kind(subarray) == JV_KIND_ARRAY);
|
||||
assert(jv_array_length(jv_copy(subarray)) == 1);
|
||||
assert(jv_number_value(jv_array_get(jv_copy(subarray), 0)) == 42);
|
||||
jv_free(subarray);
|
||||
}
|
||||
|
||||
|
||||
jv subarray = jv_array_get(jv_copy(a), 1);
|
||||
assert(jv_get_kind(subarray) == JV_KIND_ARRAY);
|
||||
assert(jv_array_length(jv_copy(subarray)) == 1);
|
||||
assert(jv_number_value(jv_array_get(jv_copy(subarray), 0)) == 42);
|
||||
|
||||
jv sub2 = jv_copy(subarray);
|
||||
sub2 = jv_array_append(sub2, jv_number(19));
|
||||
|
||||
assert(jv_get_kind(sub2) == JV_KIND_ARRAY);
|
||||
assert(jv_array_length(jv_copy(sub2)) == 2);
|
||||
assert(jv_number_value(jv_array_get(jv_copy(sub2), 0)) == 42);
|
||||
assert(jv_number_value(jv_array_get(jv_copy(sub2), 1)) == 19);
|
||||
|
||||
assert(jv_get_kind(subarray) == JV_KIND_ARRAY);
|
||||
assert(jv_array_length(jv_copy(subarray)) == 1);
|
||||
assert(jv_number_value(jv_array_get(jv_copy(subarray), 0)) == 42);
|
||||
|
||||
jv_free(subarray);
|
||||
|
||||
void* before = sub2.val.complex.ptr;
|
||||
sub2 = jv_array_append(sub2, jv_number(200));
|
||||
void* after = sub2.val.complex.ptr;
|
||||
assert(before == after);
|
||||
jv_free(sub2);
|
||||
|
||||
jv a3 = jv_array_append(jv_copy(a), jv_number(19));
|
||||
assert(jv_array_length(jv_copy(a3)) == 3);
|
||||
assert(jv_number_value(jv_array_get(jv_copy(a3), 0)) == 42);
|
||||
assert(jv_array_length(jv_array_get(jv_copy(a3), 1)) == 1);
|
||||
assert(jv_number_value(jv_array_get(jv_copy(a3), 2)) == 19);
|
||||
jv_free(a3);
|
||||
|
||||
|
||||
assert(jv_array_length(jv_copy(a)) == 2);
|
||||
assert(jv_number_value(jv_array_get(jv_copy(a), 0)) == 42);
|
||||
assert(jv_array_length(jv_array_get(jv_copy(a), 1)) == 1);
|
||||
|
||||
|
||||
jv_dump(jv_copy(a), 0); printf("\n");
|
||||
jv_free(a);
|
||||
}
|
||||
|
||||
|
||||
/// Strings
|
||||
{
|
||||
assert(jv_equal(jv_string("foo"), jv_string_sized("foo", 3)));
|
||||
char nasty[] = "foo\0";
|
||||
jv shortstr = jv_string(nasty), longstr = jv_string_sized(nasty, sizeof(nasty));
|
||||
assert(jv_string_length(shortstr) == (int)strlen(nasty));
|
||||
assert(jv_string_length(longstr) == (int)sizeof(nasty));
|
||||
|
||||
|
||||
char a1s[] = "hello", a2s[] = "hello", bs[] = "goodbye";
|
||||
jv a1 = jv_string(a1s), a2 = jv_string(a2s), b = jv_string(bs);
|
||||
assert(jv_equal(jv_copy(a1), jv_copy(a2)));
|
||||
assert(jv_equal(jv_copy(a2), jv_copy(a1)));
|
||||
assert(!jv_equal(jv_copy(a1), jv_copy(b)));
|
||||
|
||||
assert(jv_string_hash(jv_copy(a1)) == jv_string_hash(jv_copy(a1)));
|
||||
assert(jv_string_hash(jv_copy(a1)) == jv_string_hash(jv_copy(a2)));
|
||||
assert(jv_string_hash(jv_copy(b)) != jv_string_hash(jv_copy(a1)));
|
||||
jv_free(a1);
|
||||
jv_free(a2);
|
||||
jv_free(b);
|
||||
|
||||
assert(jv_equal(jv_string("hello42!"), jv_string_fmt("hello%d%s", 42, "!")));
|
||||
char big[20000];
|
||||
for (int i=0; i<(int)sizeof(big); i++) big[i] = 'a';
|
||||
big[sizeof(big)-1] = 0;
|
||||
jv str = jv_string_fmt("%s", big);
|
||||
assert(jv_string_length(jv_copy(str)) == sizeof(big) - 1);
|
||||
assert(!strcmp(big, jv_string_value(str)));
|
||||
jv_free(str);
|
||||
}
|
||||
|
||||
/// Objects
|
||||
{
|
||||
jv o1 = jv_object();
|
||||
o1 = jv_object_set(o1, jv_string("foo"), jv_number(42));
|
||||
o1 = jv_object_set(o1, jv_string("bar"), jv_number(24));
|
||||
assert(jv_number_value(jv_object_get(jv_copy(o1), jv_string("foo"))) == 42);
|
||||
assert(jv_number_value(jv_object_get(jv_copy(o1), jv_string("bar"))) == 24);
|
||||
|
||||
jv o2 = jv_object_set(jv_copy(o1), jv_string("foo"), jv_number(420));
|
||||
o2 = jv_object_set(o2, jv_string("bar"), jv_number(240));
|
||||
assert(jv_number_value(jv_object_get(jv_copy(o1), jv_string("foo"))) == 42);
|
||||
assert(jv_number_value(jv_object_get(jv_copy(o1), jv_string("bar"))) == 24);
|
||||
assert(jv_number_value(jv_object_get(jv_copy(o2), jv_string("foo"))) == 420);
|
||||
jv_free(o1);
|
||||
assert(jv_number_value(jv_object_get(jv_copy(o2), jv_string("bar"))) == 240);
|
||||
|
||||
jv_dump(jv_copy(o2), 0); printf("\n");
|
||||
|
||||
jv_free(o2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,979 @@
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "jv.h"
|
||||
|
||||
/*
|
||||
* Internal refcounting helpers
|
||||
*/
|
||||
|
||||
static void jvp_refcnt_init(jv_complex* c) {
|
||||
c->ptr->count = 1;
|
||||
}
|
||||
|
||||
static void jvp_refcnt_inc(jv_complex* c) {
|
||||
c->ptr->count++;
|
||||
}
|
||||
|
||||
static int jvp_refcnt_dec(jv_complex* c) {
|
||||
c->ptr->count--;
|
||||
return c->ptr->count == 0;
|
||||
}
|
||||
|
||||
static int jvp_refcnt_unshared(jv_complex* c) {
|
||||
assert(c->ptr->count > 0);
|
||||
return c->ptr->count == 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Simple values (true, false, null)
|
||||
*/
|
||||
|
||||
jv_kind jv_get_kind(jv x) {
|
||||
return x.kind;
|
||||
}
|
||||
|
||||
const char* jv_kind_name(jv_kind k) {
|
||||
switch (k) {
|
||||
case JV_KIND_INVALID: return "<invalid>";
|
||||
case JV_KIND_NULL: return "null";
|
||||
case JV_KIND_FALSE: return "boolean";
|
||||
case JV_KIND_TRUE: return "boolean";
|
||||
case JV_KIND_NUMBER: return "number";
|
||||
case JV_KIND_STRING: return "string";
|
||||
case JV_KIND_ARRAY: return "array";
|
||||
case JV_KIND_OBJECT: return "object";
|
||||
}
|
||||
assert(0 && "invalid kind");
|
||||
return "<unknown>";
|
||||
}
|
||||
|
||||
static const jv JV_NULL = {JV_KIND_NULL, {0}};
|
||||
static const jv JV_FALSE = {JV_KIND_FALSE, {0}};
|
||||
static const jv JV_TRUE = {JV_KIND_TRUE, {0}};
|
||||
|
||||
jv jv_true() {
|
||||
return JV_TRUE;
|
||||
}
|
||||
|
||||
jv jv_false() {
|
||||
return JV_FALSE;
|
||||
}
|
||||
|
||||
jv jv_null() {
|
||||
return JV_NULL;
|
||||
}
|
||||
|
||||
jv jv_bool(int x) {
|
||||
return x ? JV_TRUE : JV_FALSE;
|
||||
}
|
||||
|
||||
/*
|
||||
* Invalid objects, with optional error messages
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
jv_refcnt refcnt;
|
||||
jv errmsg;
|
||||
} jvp_invalid;
|
||||
|
||||
jv jv_invalid_with_msg(jv err) {
|
||||
jv x;
|
||||
x.kind = JV_KIND_INVALID;
|
||||
x.val.complex.i[0] = x.val.complex.i[1] = 0;
|
||||
jvp_invalid* i = malloc(sizeof(jvp_invalid));
|
||||
x.val.complex.ptr = &i->refcnt;
|
||||
i->refcnt.count = 1;
|
||||
i->errmsg = err;
|
||||
return x;
|
||||
}
|
||||
|
||||
jv jv_invalid() {
|
||||
return jv_invalid_with_msg(jv_null());
|
||||
}
|
||||
|
||||
jv jv_invalid_get_msg(jv inv) {
|
||||
jv x = jv_copy(((jvp_invalid*)inv.val.complex.ptr)->errmsg);
|
||||
jv_free(inv);
|
||||
return x;
|
||||
}
|
||||
|
||||
int jv_invalid_has_msg(jv inv) {
|
||||
jv msg = jv_invalid_get_msg(inv);
|
||||
int r = jv_get_kind(msg) != JV_KIND_NULL;
|
||||
jv_free(msg);
|
||||
return r;
|
||||
}
|
||||
|
||||
static void jvp_invalid_free(jv_complex* x) {
|
||||
if (jvp_refcnt_dec(x)) {
|
||||
jv_free(((jvp_invalid*)x->ptr)->errmsg);
|
||||
free(x->ptr);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Numbers
|
||||
*/
|
||||
|
||||
jv jv_number(double x) {
|
||||
jv j;
|
||||
j.kind = JV_KIND_NUMBER;
|
||||
j.val.number = x;
|
||||
return j;
|
||||
}
|
||||
|
||||
double jv_number_value(jv j) {
|
||||
assert(jv_get_kind(j) == JV_KIND_NUMBER);
|
||||
return j.val.number;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Arrays (internal helpers)
|
||||
*/
|
||||
|
||||
#define ARRAY_SIZE_ROUND_UP(n) (((n)*3)/2)
|
||||
|
||||
static int imax(int a, int b) {
|
||||
if (a>b) return a;
|
||||
else return b;
|
||||
}
|
||||
|
||||
//FIXME signed vs unsigned
|
||||
typedef struct {
|
||||
jv_refcnt refcnt;
|
||||
int length, alloc_length;
|
||||
jv elements[];
|
||||
} jvp_array;
|
||||
|
||||
static jvp_array* jvp_array_ptr(jv_complex* a) {
|
||||
return (jvp_array*)a->ptr;
|
||||
}
|
||||
|
||||
static jvp_array* jvp_array_alloc(unsigned size) {
|
||||
jvp_array* a = malloc(sizeof(jvp_array) + sizeof(jv) * size);
|
||||
a->refcnt.count = 1;
|
||||
a->length = 0;
|
||||
a->alloc_length = size;
|
||||
return a;
|
||||
}
|
||||
|
||||
static jv_complex jvp_array_new(unsigned size) {
|
||||
jv_complex r = {&jvp_array_alloc(size)->refcnt, {0, 0}};
|
||||
return r;
|
||||
}
|
||||
|
||||
static void jvp_array_free(jv_complex* a) {
|
||||
if (jvp_refcnt_dec(a)) {
|
||||
jvp_array* array = jvp_array_ptr(a);
|
||||
for (int i=0; i<array->length; i++) {
|
||||
jv_free(array->elements[i]);
|
||||
}
|
||||
free(array);
|
||||
}
|
||||
}
|
||||
|
||||
static int jvp_array_length(jv_complex* a) {
|
||||
return a->i[1] - a->i[0];
|
||||
}
|
||||
|
||||
static jv* jvp_array_read(jv_complex* a, int i) {
|
||||
if (i >= 0 && i < jvp_array_length(a)) {
|
||||
jvp_array* array = jvp_array_ptr(a);
|
||||
assert(i + a->i[0] < array->length);
|
||||
return &array->elements[i + a->i[0]];
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static jv* jvp_array_write(jv_complex* a, int i) {
|
||||
assert(i >= 0);
|
||||
jvp_array* array = jvp_array_ptr(a);
|
||||
|
||||
int pos = i + a->i[0];
|
||||
if (pos < array->alloc_length) {
|
||||
// maybe we can update it in-place
|
||||
// FIXME: this "optimisation" can cause circular references
|
||||
#if 0
|
||||
int can_write_past_end =
|
||||
array->length <= pos && /* the end of this array has never been used */
|
||||
a->i[1] == array->length; /* the current slice sees the end of the array */
|
||||
#endif
|
||||
int can_write_past_end = 0;
|
||||
if (can_write_past_end || jvp_refcnt_unshared(a)) {
|
||||
// extend the array
|
||||
for (int j = array->length; j <= pos; j++) {
|
||||
array->elements[j] = JV_NULL;
|
||||
}
|
||||
array->length = imax(pos + 1, array->length);
|
||||
a->i[1] = imax(pos + 1, array->length);
|
||||
return &array->elements[pos];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int new_length = imax(i + 1, jvp_array_length(a));
|
||||
jvp_array* new_array = jvp_array_alloc(ARRAY_SIZE_ROUND_UP(new_length));
|
||||
int j;
|
||||
for (j = 0; j < jvp_array_length(a); j++) {
|
||||
new_array->elements[j] = jv_copy(array->elements[j + a->i[0]]);
|
||||
}
|
||||
for (; j < new_length; j++) {
|
||||
new_array->elements[j] = JV_NULL;
|
||||
}
|
||||
new_array->length = new_length;
|
||||
jvp_array_free(a);
|
||||
a->ptr = &new_array->refcnt;
|
||||
a->i[0] = 0;
|
||||
a->i[1] = new_length;
|
||||
return &new_array->elements[i];
|
||||
}
|
||||
|
||||
static int jvp_array_equal(jv_complex* a, jv_complex* b) {
|
||||
if (jvp_array_length(a) != jvp_array_length(b))
|
||||
return 0;
|
||||
if (jvp_array_ptr(a) == jvp_array_ptr(b) &&
|
||||
a->i[0] == b->i[0])
|
||||
return 1;
|
||||
for (int i=0; i<jvp_array_length(a); i++) {
|
||||
if (!jv_equal(jv_copy(*jvp_array_read(a, i)),
|
||||
jv_copy(*jvp_array_read(b,i))))
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static jv_complex jvp_array_slice(jv_complex* a, int start, int end) {
|
||||
// FIXME: maybe slice should reallocate if the slice is small enough
|
||||
assert(start <= end);
|
||||
jvp_array* array = jvp_array_ptr(a);
|
||||
assert(a->i[1] + end < array->length);
|
||||
jv_complex slice = *a;
|
||||
slice.i[0] += start;
|
||||
slice.i[1] = slice.i[0] + (end - start);
|
||||
return slice;
|
||||
}
|
||||
|
||||
/*
|
||||
* Arrays (public interface)
|
||||
*/
|
||||
|
||||
jv jv_array_sized(int n) {
|
||||
jv j;
|
||||
j.kind = JV_KIND_ARRAY;
|
||||
j.val.complex = jvp_array_new(n);
|
||||
return j;
|
||||
}
|
||||
|
||||
jv jv_array() {
|
||||
return jv_array_sized(16);
|
||||
}
|
||||
|
||||
int jv_array_length(jv j) {
|
||||
assert(jv_get_kind(j) == JV_KIND_ARRAY);
|
||||
int len = jvp_array_length(&j.val.complex);
|
||||
jv_free(j);
|
||||
return len;
|
||||
}
|
||||
|
||||
jv jv_array_get(jv j, int idx) {
|
||||
assert(jv_get_kind(j) == JV_KIND_ARRAY);
|
||||
jv* slot = jvp_array_read(&j.val.complex, idx);
|
||||
jv val;
|
||||
if (slot) {
|
||||
val = jv_copy(*slot);
|
||||
} else {
|
||||
val = jv_invalid();
|
||||
}
|
||||
jv_free(j);
|
||||
return val;
|
||||
}
|
||||
|
||||
jv jv_array_set(jv j, int idx, jv val) {
|
||||
assert(jv_get_kind(j) == JV_KIND_ARRAY);
|
||||
// copy/free of val,j coalesced
|
||||
jv* slot = jvp_array_write(&j.val.complex, idx);
|
||||
jv_free(*slot);
|
||||
*slot = val;
|
||||
return j;
|
||||
}
|
||||
|
||||
jv jv_array_append(jv j, jv val) {
|
||||
// copy/free of val,j coalesced
|
||||
return jv_array_set(j, jv_array_length(jv_copy(j)), val);
|
||||
}
|
||||
|
||||
jv jv_array_concat(jv a, jv b) {
|
||||
assert(jv_get_kind(a) == JV_KIND_ARRAY);
|
||||
assert(jv_get_kind(b) == JV_KIND_ARRAY);
|
||||
|
||||
// FIXME: could be much faster
|
||||
for (int i=0; i<jv_array_length(jv_copy(b)); i++) {
|
||||
a = jv_array_append(a, jv_array_get(jv_copy(b), i));
|
||||
}
|
||||
jv_free(b);
|
||||
return a;
|
||||
}
|
||||
|
||||
jv jv_array_slice(jv a, int start, int end) {
|
||||
assert(jv_get_kind(a) == JV_KIND_ARRAY);
|
||||
// copy/free of a coalesced
|
||||
a.val.complex = jvp_array_slice(&a.val.complex, start, end);
|
||||
return a;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Strings (internal helpers)
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
jv_refcnt refcnt;
|
||||
uint32_t hash;
|
||||
// high 31 bits are length, low bit is a flag
|
||||
// indicating whether hash has been computed.
|
||||
uint32_t length_hashed;
|
||||
uint32_t alloc_length;
|
||||
char data[];
|
||||
} jvp_string;
|
||||
|
||||
static jvp_string* jvp_string_ptr(jv_complex* a) {
|
||||
return (jvp_string*)a->ptr;
|
||||
}
|
||||
|
||||
static jvp_string* jvp_string_alloc(uint32_t size) {
|
||||
jvp_string* s = malloc(sizeof(jvp_string) + size + 1);
|
||||
s->refcnt.count = 1;
|
||||
s->alloc_length = size;
|
||||
return s;
|
||||
}
|
||||
|
||||
static jv_complex jvp_string_new(const char* data, uint32_t length) {
|
||||
jvp_string* s = jvp_string_alloc(length);
|
||||
s->length_hashed = length << 1;
|
||||
memcpy(s->data, data, length);
|
||||
s->data[length] = 0;
|
||||
jv_complex r = {&s->refcnt, {0,0}};
|
||||
return r;
|
||||
}
|
||||
|
||||
static void jvp_string_free(jv_complex* s) {
|
||||
if (jvp_refcnt_dec(s)) {
|
||||
jvp_string* str = jvp_string_ptr(s);
|
||||
free(str);
|
||||
}
|
||||
}
|
||||
|
||||
static void jvp_string_free_p(jvp_string* s) {
|
||||
jv_complex p = {&s->refcnt,{0,0}};
|
||||
jvp_string_free(&p);
|
||||
}
|
||||
|
||||
static jvp_string* jvp_string_copy_p(jvp_string* s) {
|
||||
jv_complex p = {&s->refcnt,{0,0}};
|
||||
jvp_refcnt_inc(&p);
|
||||
return s;
|
||||
}
|
||||
|
||||
static uint32_t jvp_string_length(jvp_string* s) {
|
||||
return s->length_hashed >> 1;
|
||||
}
|
||||
|
||||
static uint32_t jvp_string_remaining_space(jvp_string* s) {
|
||||
assert(s->alloc_length >= jvp_string_length(s));
|
||||
uint32_t r = s->alloc_length - jvp_string_length(s);
|
||||
return r;
|
||||
}
|
||||
|
||||
static void jvp_string_append(jv_complex* string, const char* data, uint32_t len) {
|
||||
jvp_string* s = jvp_string_ptr(string);
|
||||
uint32_t currlen = jvp_string_length(s);
|
||||
|
||||
if (jvp_refcnt_unshared(string) &&
|
||||
jvp_string_remaining_space(s) >= len) {
|
||||
// the next string fits at the end of a
|
||||
memcpy(s->data + currlen, data, len);
|
||||
s->data[currlen + len] = 0;
|
||||
s->length_hashed = (currlen + len) << 1;
|
||||
} else {
|
||||
// allocate a bigger buffer and copy
|
||||
uint32_t allocsz = (currlen + len) * 2;
|
||||
if (allocsz < 32) allocsz = 32;
|
||||
jvp_string* news = jvp_string_alloc(allocsz);
|
||||
news->length_hashed = (currlen + len) << 1;
|
||||
memcpy(news->data, s->data, currlen);
|
||||
memcpy(news->data + currlen, data, len);
|
||||
news->data[currlen + len] = 0;
|
||||
jvp_string_free(string);
|
||||
jv_complex r = {&news->refcnt, {0,0}};
|
||||
*string = r;
|
||||
}
|
||||
}
|
||||
|
||||
static const uint32_t HASH_SEED = 0x432A9843;
|
||||
|
||||
static uint32_t rotl32 (uint32_t x, int8_t r){
|
||||
return (x << r) | (x >> (32 - r));
|
||||
}
|
||||
|
||||
static uint32_t jvp_string_hash(jvp_string* str) {
|
||||
if (str->length_hashed & 1)
|
||||
return str->hash;
|
||||
|
||||
/* The following is based on MurmurHash3.
|
||||
MurmurHash3 was written by Austin Appleby, and is placed
|
||||
in the public domain. */
|
||||
|
||||
const uint8_t* data = (const uint8_t*)str->data;
|
||||
int len = (int)jvp_string_length(str);
|
||||
const int nblocks = len / 4;
|
||||
|
||||
uint32_t h1 = HASH_SEED;
|
||||
|
||||
const uint32_t c1 = 0xcc9e2d51;
|
||||
const uint32_t c2 = 0x1b873593;
|
||||
const uint32_t* blocks = (const uint32_t *)(data + nblocks*4);
|
||||
|
||||
for(int i = -nblocks; i; i++) {
|
||||
uint32_t k1 = blocks[i]; //FIXME: endianness/alignment
|
||||
|
||||
k1 *= c1;
|
||||
k1 = rotl32(k1,15);
|
||||
k1 *= c2;
|
||||
|
||||
h1 ^= k1;
|
||||
h1 = rotl32(h1,13);
|
||||
h1 = h1*5+0xe6546b64;
|
||||
}
|
||||
|
||||
const uint8_t* tail = (const uint8_t*)(data + nblocks*4);
|
||||
|
||||
uint32_t k1 = 0;
|
||||
|
||||
switch(len & 3) {
|
||||
case 3: k1 ^= tail[2] << 16;
|
||||
case 2: k1 ^= tail[1] << 8;
|
||||
case 1: k1 ^= tail[0];
|
||||
k1 *= c1; k1 = rotl32(k1,15); k1 *= c2; h1 ^= k1;
|
||||
}
|
||||
|
||||
h1 ^= len;
|
||||
|
||||
h1 ^= h1 >> 16;
|
||||
h1 *= 0x85ebca6b;
|
||||
h1 ^= h1 >> 13;
|
||||
h1 *= 0xc2b2ae35;
|
||||
h1 ^= h1 >> 16;
|
||||
|
||||
str->length_hashed |= 1;
|
||||
str->hash = h1;
|
||||
|
||||
return h1;
|
||||
}
|
||||
|
||||
static int jvp_string_equal_hashed(jvp_string* a, jvp_string* b) {
|
||||
assert(a->length_hashed & 1);
|
||||
assert(b->length_hashed & 1);
|
||||
if (a == b) return 1;
|
||||
if (a->hash != b->hash) return 0;
|
||||
if (a->length_hashed != b->length_hashed) return 0;
|
||||
return memcmp(a->data, b->data, jvp_string_length(a)) == 0;
|
||||
}
|
||||
|
||||
static int jvp_string_equal(jv_complex* a, jv_complex* b) {
|
||||
jvp_string* stra = jvp_string_ptr(a);
|
||||
jvp_string* strb = jvp_string_ptr(b);
|
||||
if (jvp_string_length(stra) != jvp_string_length(strb)) return 0;
|
||||
return memcmp(stra->data, strb->data, jvp_string_length(stra)) == 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Strings (public API)
|
||||
*/
|
||||
|
||||
jv jv_string_sized(const char* str, int len) {
|
||||
jv j;
|
||||
j.kind = JV_KIND_STRING;
|
||||
j.val.complex = jvp_string_new(str, len);
|
||||
return j;
|
||||
}
|
||||
|
||||
jv jv_string(const char* str) {
|
||||
return jv_string_sized(str, strlen(str));
|
||||
}
|
||||
|
||||
int jv_string_length(jv j) {
|
||||
assert(jv_get_kind(j) == JV_KIND_STRING);
|
||||
int r = jvp_string_length(jvp_string_ptr(&j.val.complex));
|
||||
jv_free(j);
|
||||
return r;
|
||||
}
|
||||
|
||||
uint32_t jv_string_hash(jv j) {
|
||||
assert(jv_get_kind(j) == JV_KIND_STRING);
|
||||
uint32_t hash = jvp_string_hash(jvp_string_ptr(&j.val.complex));
|
||||
jv_free(j);
|
||||
return hash;
|
||||
}
|
||||
|
||||
const char* jv_string_value(jv j) {
|
||||
assert(jv_get_kind(j) == JV_KIND_STRING);
|
||||
return jvp_string_ptr(&j.val.complex)->data;
|
||||
}
|
||||
|
||||
jv jv_string_concat(jv a, jv b) {
|
||||
jvp_string* sb = jvp_string_ptr(&b.val.complex);
|
||||
jvp_string_append(&a.val.complex, sb->data, jvp_string_length(sb));
|
||||
jv_free(b);
|
||||
return a;
|
||||
}
|
||||
|
||||
jv jv_string_append_buf(jv a, const char* buf, int len) {
|
||||
jvp_string_append(&a.val.complex, buf, len);
|
||||
return a;
|
||||
}
|
||||
|
||||
jv jv_string_append_str(jv a, const char* str) {
|
||||
return jv_string_append_buf(a, str, strlen(str));
|
||||
}
|
||||
|
||||
jv jv_string_fmt(const char* fmt, ...) {
|
||||
int size = 1024;
|
||||
while (1) {
|
||||
char* buf = malloc(size);
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
int n = vsnprintf(buf, size, fmt, args);
|
||||
va_end(args);
|
||||
if (n < size) {
|
||||
jv ret = jv_string_sized(buf, n);
|
||||
free(buf);
|
||||
return ret;
|
||||
} else {
|
||||
free(buf);
|
||||
size = n * 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Objects (internal helpers)
|
||||
*/
|
||||
|
||||
struct object_slot {
|
||||
int next;
|
||||
jvp_string* string;
|
||||
uint32_t hash;
|
||||
jv value;
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
jv_refcnt refcnt;
|
||||
int first_free;
|
||||
struct object_slot elements[];
|
||||
} jvp_object;
|
||||
|
||||
|
||||
/* warning: complex justification of alignment */
|
||||
static jv_complex jvp_object_new(int size) {
|
||||
// Allocates an object of (size) slots and (size*2) hash buckets.
|
||||
|
||||
// size must be a power of two
|
||||
assert(size > 0 && (size & (size - 1)) == 0);
|
||||
jvp_object* obj = malloc(sizeof(jvp_object) +
|
||||
sizeof(struct object_slot) * size +
|
||||
sizeof(int) * (size * 2));
|
||||
obj->refcnt.count = 1;
|
||||
for (int i=0; i<size; i++) {
|
||||
obj->elements[i].next = i - 1;
|
||||
obj->elements[i].string = 0;
|
||||
obj->elements[i].hash = 0;
|
||||
obj->elements[i].value = JV_NULL;
|
||||
}
|
||||
obj->first_free = size - 1;
|
||||
int* hashbuckets = (int*)(&obj->elements[size]);
|
||||
jv_complex r = {&obj->refcnt,
|
||||
{size*2 - 1, (char*)hashbuckets - (char*)obj}};
|
||||
for (int i=0; i<size*2; i++) {
|
||||
hashbuckets[i] = -1;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
static jvp_object* jvp_object_ptr(jv_complex* o) {
|
||||
return (jvp_object*)o->ptr;
|
||||
}
|
||||
|
||||
static uint32_t jvp_object_mask(jv_complex* o) {
|
||||
return o->i[0];
|
||||
}
|
||||
|
||||
static int jvp_object_size(jv_complex* o) {
|
||||
return (o->i[0] + 1) >> 1;
|
||||
}
|
||||
|
||||
static int* jvp_object_buckets(jv_complex* o) {
|
||||
int* buckets = (int*)((char*)o->ptr + o->i[1]);
|
||||
assert(buckets == (int*)&jvp_object_ptr(o)->elements[jvp_object_size(o)]);
|
||||
return buckets;
|
||||
}
|
||||
|
||||
static int* jvp_object_find_bucket(jv_complex* object, jvp_string* key) {
|
||||
return jvp_object_buckets(object) + (jvp_object_mask(object) & jvp_string_hash(key));
|
||||
}
|
||||
|
||||
static struct object_slot* jvp_object_get_slot(jv_complex* object, int slot) {
|
||||
assert(slot == -1 || (slot >= 0 && slot < jvp_object_size(object)));
|
||||
if (slot == -1) return 0;
|
||||
else return &jvp_object_ptr(object)->elements[slot];
|
||||
}
|
||||
|
||||
static struct object_slot* jvp_object_next_slot(jv_complex* object, struct object_slot* slot) {
|
||||
return jvp_object_get_slot(object, slot->next);
|
||||
}
|
||||
|
||||
static struct object_slot* jvp_object_find_slot(jv_complex* object, jvp_string* keystr, int* bucket) {
|
||||
for (struct object_slot* curr = jvp_object_get_slot(object, *bucket);
|
||||
curr;
|
||||
curr = jvp_object_next_slot(object, curr)) {
|
||||
if (jvp_string_equal_hashed(keystr, curr->string)) {
|
||||
return curr;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct object_slot* jvp_object_add_slot(jv_complex* object, jvp_string* key, int* bucket) {
|
||||
jvp_object* o = jvp_object_ptr(object);
|
||||
int newslot_idx = o->first_free;
|
||||
struct object_slot* newslot = jvp_object_get_slot(object, newslot_idx);
|
||||
if (newslot == 0) return 0;
|
||||
o->first_free = newslot->next;
|
||||
newslot->next = *bucket;
|
||||
*bucket = newslot_idx;
|
||||
newslot->hash = jvp_string_hash(key);
|
||||
newslot->string = key;
|
||||
return newslot;
|
||||
}
|
||||
|
||||
static void jvp_object_free_slot(jv_complex* object, struct object_slot* slot) {
|
||||
jvp_object* o = jvp_object_ptr(object);
|
||||
slot->next = o->first_free;
|
||||
assert(slot->string);
|
||||
jvp_string_free_p(slot->string);
|
||||
slot->string = 0;
|
||||
jv_free(slot->value);
|
||||
o->first_free = slot - jvp_object_get_slot(object, 0);
|
||||
}
|
||||
|
||||
static jv* jvp_object_read(jv_complex* object, jvp_string* key) {
|
||||
int* bucket = jvp_object_find_bucket(object, key);
|
||||
struct object_slot* slot = jvp_object_find_slot(object, key, bucket);
|
||||
if (slot == 0) return 0;
|
||||
else return &slot->value;
|
||||
}
|
||||
|
||||
static void jvp_object_free(jv_complex* o) {
|
||||
if (jvp_refcnt_dec(o)) {
|
||||
for (int i=0; i<jvp_object_size(o); i++) {
|
||||
struct object_slot* slot = jvp_object_get_slot(o, i);
|
||||
if (slot->string) {
|
||||
jvp_string_free_p(slot->string);
|
||||
jv_free(slot->value);
|
||||
}
|
||||
}
|
||||
free(jvp_object_ptr(o));
|
||||
}
|
||||
}
|
||||
|
||||
static void jvp_object_rehash(jv_complex* object) {
|
||||
assert(jvp_refcnt_unshared(object));
|
||||
int size = jvp_object_size(object);
|
||||
jv_complex new_object = jvp_object_new(size * 2);
|
||||
for (int i=0; i<size; i++) {
|
||||
struct object_slot* slot = jvp_object_get_slot(object, i);
|
||||
if (!slot->string) continue;
|
||||
|
||||
int* new_bucket = jvp_object_find_bucket(&new_object, slot->string);
|
||||
assert(!jvp_object_find_slot(&new_object, slot->string, new_bucket));
|
||||
struct object_slot* new_slot = jvp_object_add_slot(&new_object, slot->string, new_bucket);
|
||||
assert(new_slot);
|
||||
new_slot->value = slot->value;
|
||||
}
|
||||
// references are transported, just drop the old table
|
||||
free(jvp_object_ptr(object));
|
||||
*object = new_object;
|
||||
}
|
||||
|
||||
static void jvp_object_unshare(jv_complex* object) {
|
||||
if (jvp_refcnt_unshared(object))
|
||||
return;
|
||||
|
||||
jv_complex new_object = jvp_object_new(jvp_object_size(object));
|
||||
jvp_object_ptr(&new_object)->first_free = jvp_object_ptr(object)->first_free;
|
||||
for (int i=0; i<jvp_object_size(&new_object); i++) {
|
||||
struct object_slot* old_slot = jvp_object_get_slot(object, i);
|
||||
struct object_slot* new_slot = jvp_object_get_slot(&new_object, i);
|
||||
*new_slot = *old_slot;
|
||||
if (old_slot->string) {
|
||||
new_slot->string = jvp_string_copy_p(old_slot->string);
|
||||
new_slot->value = jv_copy(old_slot->value);
|
||||
}
|
||||
}
|
||||
|
||||
int* old_buckets = jvp_object_buckets(object);
|
||||
int* new_buckets = jvp_object_buckets(&new_object);
|
||||
memcpy(new_buckets, old_buckets, sizeof(int) * jvp_object_size(&new_object)*2);
|
||||
|
||||
jvp_object_free(object);
|
||||
*object = new_object;
|
||||
assert(jvp_refcnt_unshared(object));
|
||||
}
|
||||
|
||||
static jv* jvp_object_write(jv_complex* object, jvp_string* key) {
|
||||
jvp_object_unshare(object);
|
||||
int* bucket = jvp_object_find_bucket(object, key);
|
||||
struct object_slot* slot = jvp_object_find_slot(object, key, bucket);
|
||||
if (slot) {
|
||||
// already has the key
|
||||
jvp_string_free_p(key);
|
||||
return &slot->value;
|
||||
}
|
||||
slot = jvp_object_add_slot(object, key, bucket);
|
||||
if (slot) {
|
||||
slot->value = jv_invalid();
|
||||
} else {
|
||||
jvp_object_rehash(object);
|
||||
bucket = jvp_object_find_bucket(object, key);
|
||||
assert(!jvp_object_find_slot(object, key, bucket));
|
||||
slot = jvp_object_add_slot(object, key, bucket);
|
||||
assert(slot);
|
||||
slot->value = jv_invalid();
|
||||
}
|
||||
return &slot->value;
|
||||
}
|
||||
|
||||
static int jvp_object_delete(jv_complex* object, jvp_string* key) {
|
||||
jvp_object_unshare(object);
|
||||
int* bucket = jvp_object_find_bucket(object, key);
|
||||
int* prev_ptr = bucket;
|
||||
for (struct object_slot* curr = jvp_object_get_slot(object, *bucket);
|
||||
curr;
|
||||
curr = jvp_object_next_slot(object, curr)) {
|
||||
if (jvp_string_equal_hashed(key, curr->string)) {
|
||||
*prev_ptr = curr->next;
|
||||
jvp_object_free_slot(object, curr);
|
||||
return 1;
|
||||
}
|
||||
prev_ptr = &curr->next;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int jvp_object_length(jv_complex* object) {
|
||||
int n = 0;
|
||||
for (int i=0; i<jvp_object_size(object); i++) {
|
||||
struct object_slot* slot = jvp_object_get_slot(object, i);
|
||||
if (slot->string) n++;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
static int jvp_object_equal(jv_complex* o1, jv_complex* o2) {
|
||||
int len2 = jvp_object_length(o2);
|
||||
int len1 = 0;
|
||||
for (int i=0; i<jvp_object_size(o1); i++) {
|
||||
struct object_slot* slot = jvp_object_get_slot(o1, i);
|
||||
if (!slot->string) continue;
|
||||
jv* slot2 = jvp_object_read(o2, slot->string);
|
||||
if (!slot2) return 0;
|
||||
// FIXME: do less refcounting here
|
||||
if (!jv_equal(jv_copy(slot->value), jv_copy(*slot2))) return 0;
|
||||
len1++;
|
||||
}
|
||||
return len1 == len2;
|
||||
}
|
||||
|
||||
/*
|
||||
* Objects (public interface)
|
||||
*/
|
||||
#define DEFAULT_OBJECT_SIZE 8
|
||||
jv jv_object() {
|
||||
jv j;
|
||||
j.kind = JV_KIND_OBJECT;
|
||||
j.val.complex = jvp_object_new(8);
|
||||
return j;
|
||||
}
|
||||
|
||||
jv jv_object_get(jv object, jv key) {
|
||||
assert(jv_get_kind(object) == JV_KIND_OBJECT);
|
||||
assert(jv_get_kind(key) == JV_KIND_STRING);
|
||||
jv* slot = jvp_object_read(&object.val.complex, jvp_string_ptr(&key.val.complex));
|
||||
jv val;
|
||||
if (slot) {
|
||||
val = jv_copy(*slot);
|
||||
} else {
|
||||
val = jv_invalid();
|
||||
}
|
||||
jv_free(object);
|
||||
jv_free(key);
|
||||
return val;
|
||||
}
|
||||
|
||||
jv jv_object_set(jv object, jv key, jv value) {
|
||||
assert(jv_get_kind(object) == JV_KIND_OBJECT);
|
||||
assert(jv_get_kind(key) == JV_KIND_STRING);
|
||||
// copy/free of object, key, value coalesced
|
||||
jv* slot = jvp_object_write(&object.val.complex, jvp_string_ptr(&key.val.complex));
|
||||
jv_free(*slot);
|
||||
*slot = value;
|
||||
return object;
|
||||
}
|
||||
|
||||
jv jv_object_delete(jv object, jv key) {
|
||||
assert(jv_get_kind(object) == JV_KIND_OBJECT);
|
||||
assert(jv_get_kind(key) == JV_KIND_STRING);
|
||||
jvp_object_delete(&object.val.complex, jvp_string_ptr(&key.val.complex));
|
||||
jv_free(key);
|
||||
return object;
|
||||
}
|
||||
|
||||
int jv_object_length(jv object) {
|
||||
assert(jv_get_kind(object) == JV_KIND_OBJECT);
|
||||
int n = jvp_object_length(&object.val.complex);
|
||||
jv_free(object);
|
||||
return n;
|
||||
}
|
||||
|
||||
jv jv_object_merge(jv a, jv b) {
|
||||
assert(jv_get_kind(a) == JV_KIND_OBJECT);
|
||||
jv_object_foreach(i, b) {
|
||||
a = jv_object_set(a,
|
||||
jv_object_iter_key(b, i),
|
||||
jv_object_iter_value(b, i));
|
||||
}
|
||||
jv_free(b);
|
||||
return a;
|
||||
}
|
||||
|
||||
/*
|
||||
* Object iteration (internal helpers)
|
||||
*/
|
||||
|
||||
enum { ITER_FINISHED = -2 };
|
||||
|
||||
int jv_object_iter_valid(jv object, int i) {
|
||||
return i != ITER_FINISHED;
|
||||
}
|
||||
|
||||
int jv_object_iter(jv object) {
|
||||
assert(jv_get_kind(object) == JV_KIND_OBJECT);
|
||||
return jv_object_iter_next(object, -1);
|
||||
}
|
||||
|
||||
int jv_object_iter_next(jv object, int iter) {
|
||||
assert(jv_get_kind(object) == JV_KIND_OBJECT);
|
||||
assert(iter != ITER_FINISHED);
|
||||
jv_complex* o = &object.val.complex;
|
||||
struct object_slot* slot;
|
||||
do {
|
||||
iter++;
|
||||
if (iter >= jvp_object_size(o))
|
||||
return ITER_FINISHED;
|
||||
slot = jvp_object_get_slot(o, iter);
|
||||
} while (!slot->string);
|
||||
return iter;
|
||||
}
|
||||
|
||||
jv jv_object_iter_key(jv object, int iter) {
|
||||
jvp_string* s = jvp_object_get_slot(&object.val.complex, iter)->string;
|
||||
assert(s);
|
||||
jv j;
|
||||
j.kind = JV_KIND_STRING;
|
||||
j.val.complex.ptr = &s->refcnt;
|
||||
j.val.complex.i[0] = 0;
|
||||
j.val.complex.i[1] = 0;
|
||||
return jv_copy(j);
|
||||
}
|
||||
|
||||
jv jv_object_iter_value(jv object, int iter) {
|
||||
return jv_copy(jvp_object_get_slot(&object.val.complex, iter)->value);
|
||||
}
|
||||
|
||||
/*
|
||||
* Memory management
|
||||
*/
|
||||
jv jv_copy(jv j) {
|
||||
if (jv_get_kind(j) == JV_KIND_ARRAY ||
|
||||
jv_get_kind(j) == JV_KIND_STRING ||
|
||||
jv_get_kind(j) == JV_KIND_OBJECT ||
|
||||
jv_get_kind(j) == JV_KIND_INVALID) {
|
||||
jvp_refcnt_inc(&j.val.complex);
|
||||
}
|
||||
return j;
|
||||
}
|
||||
|
||||
void jv_free(jv j) {
|
||||
if (jv_get_kind(j) == JV_KIND_ARRAY) {
|
||||
jvp_array_free(&j.val.complex);
|
||||
} else if (jv_get_kind(j) == JV_KIND_STRING) {
|
||||
jvp_string_free(&j.val.complex);
|
||||
} else if (jv_get_kind(j) == JV_KIND_OBJECT) {
|
||||
jvp_object_free(&j.val.complex);
|
||||
} else if (jv_get_kind(j) == JV_KIND_INVALID) {
|
||||
jvp_invalid_free(&j.val.complex);
|
||||
}
|
||||
}
|
||||
|
||||
int jv_get_refcnt(jv j) {
|
||||
switch (jv_get_kind(j)) {
|
||||
case JV_KIND_ARRAY:
|
||||
case JV_KIND_STRING:
|
||||
case JV_KIND_OBJECT:
|
||||
return j.val.complex.ptr->count;
|
||||
default:
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Higher-level operations
|
||||
*/
|
||||
|
||||
int jv_equal(jv a, jv b) {
|
||||
int r;
|
||||
if (jv_get_kind(a) != jv_get_kind(b)) {
|
||||
r = 0;
|
||||
} else if (jv_get_kind(a) == JV_KIND_NUMBER) {
|
||||
r = jv_number_value(a) == jv_number_value(b);
|
||||
} else if (a.val.complex.ptr == b.val.complex.ptr &&
|
||||
a.val.complex.i[0] == b.val.complex.i[0] &&
|
||||
a.val.complex.i[1] == b.val.complex.i[1]) {
|
||||
r = 1;
|
||||
} else {
|
||||
switch (jv_get_kind(a)) {
|
||||
case JV_KIND_ARRAY:
|
||||
r = jvp_array_equal(&a.val.complex, &b.val.complex);
|
||||
break;
|
||||
case JV_KIND_STRING:
|
||||
r = jvp_string_equal(&a.val.complex, &b.val.complex);
|
||||
break;
|
||||
case JV_KIND_OBJECT:
|
||||
r = jvp_object_equal(&a.val.complex, &b.val.complex);
|
||||
break;
|
||||
default:
|
||||
r = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
jv_free(a);
|
||||
jv_free(b);
|
||||
return r;
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
#ifndef JV_H
|
||||
#define JV_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
|
||||
typedef enum {
|
||||
JV_KIND_INVALID,
|
||||
JV_KIND_NULL,
|
||||
JV_KIND_FALSE,
|
||||
JV_KIND_TRUE,
|
||||
JV_KIND_NUMBER,
|
||||
JV_KIND_STRING,
|
||||
JV_KIND_ARRAY,
|
||||
JV_KIND_OBJECT
|
||||
} jv_kind;
|
||||
|
||||
typedef struct {
|
||||
size_t count;
|
||||
} jv_refcnt;
|
||||
|
||||
typedef struct{
|
||||
jv_refcnt* ptr;
|
||||
int i[2];
|
||||
} jv_complex;
|
||||
|
||||
typedef struct {
|
||||
jv_kind kind;
|
||||
union {
|
||||
double number;
|
||||
jv_complex complex;
|
||||
} val;
|
||||
} jv;
|
||||
|
||||
/*
|
||||
* All jv_* functions consume (decref) input and produce (incref) output
|
||||
* Except jv_copy
|
||||
*/
|
||||
|
||||
jv_kind jv_get_kind(jv);
|
||||
const char* jv_kind_name(jv_kind);
|
||||
static int jv_is_valid(jv x) { return jv_get_kind(x) != JV_KIND_INVALID; }
|
||||
|
||||
jv jv_copy(jv);
|
||||
void jv_free(jv);
|
||||
|
||||
int jv_equal(jv, jv);
|
||||
|
||||
jv jv_invalid();
|
||||
jv jv_invalid_with_msg(jv);
|
||||
jv jv_invalid_get_msg(jv);
|
||||
int jv_invalid_has_msg(jv);
|
||||
|
||||
|
||||
jv jv_null();
|
||||
jv jv_true();
|
||||
jv jv_false();
|
||||
jv jv_bool(int);
|
||||
|
||||
jv jv_number(double);
|
||||
double jv_number_value(jv);
|
||||
|
||||
jv jv_array();
|
||||
jv jv_array_sized(int);
|
||||
int jv_array_length(jv);
|
||||
jv jv_array_get(jv, int);
|
||||
jv jv_array_set(jv, int, jv);
|
||||
jv jv_array_append(jv, jv);
|
||||
jv jv_array_concat(jv, jv);
|
||||
jv jv_array_slice(jv, int, int);
|
||||
|
||||
|
||||
jv jv_string(const char*);
|
||||
jv jv_string_sized(const char*, int);
|
||||
int jv_string_length(jv);
|
||||
uint32_t jv_string_hash(jv);
|
||||
const char* jv_string_value(jv);
|
||||
jv jv_string_concat(jv, jv);
|
||||
jv jv_string_fmt(const char*, ...);
|
||||
jv jv_string_append_buf(jv a, const char* buf, int len);
|
||||
jv jv_string_append_str(jv a, const char* str);
|
||||
|
||||
jv jv_object();
|
||||
jv jv_object_get(jv object, jv key);
|
||||
jv jv_object_set(jv object, jv key, jv value);
|
||||
jv jv_object_delete(jv object, jv key);
|
||||
int jv_object_length(jv object);
|
||||
jv jv_object_merge(jv, jv);
|
||||
|
||||
int jv_object_iter(jv);
|
||||
int jv_object_iter_next(jv, int);
|
||||
int jv_object_iter_valid(jv, int);
|
||||
jv jv_object_iter_key(jv, int);
|
||||
jv jv_object_iter_value(jv, int);
|
||||
#define jv_object_foreach(i,t) \
|
||||
for (int i = jv_object_iter(t); \
|
||||
jv_object_iter_valid(t, i); \
|
||||
i = jv_object_iter_next(t, i)) \
|
||||
|
||||
|
||||
|
||||
int jv_get_refcnt(jv);
|
||||
|
||||
enum { JV_PRINT_PRETTY = 1, JV_PRINT_ASCII = 2 };
|
||||
void jv_dump(jv, int flags);
|
||||
jv jv_dump_string(jv, int flags);
|
||||
|
||||
jv jv_parse(const char* string);
|
||||
jv jv_parse_sized(const char* string, int length);
|
||||
|
||||
|
||||
static jv jv_lookup(jv t, jv k) {
|
||||
jv v;
|
||||
if (jv_get_kind(t) == JV_KIND_OBJECT && jv_get_kind(k) == JV_KIND_STRING) {
|
||||
v = jv_object_get(t, k);
|
||||
if (!jv_is_valid(v)) {
|
||||
jv_free(v);
|
||||
v = jv_null();
|
||||
}
|
||||
} else if (jv_get_kind(t) == JV_KIND_ARRAY && jv_get_kind(k) == JV_KIND_NUMBER) {
|
||||
// FIXME: don't do lookup for noninteger index
|
||||
v = jv_array_get(t, (int)jv_number_value(k));
|
||||
if (!jv_is_valid(v)) {
|
||||
jv_free(v);
|
||||
v = jv_null();
|
||||
}
|
||||
} else if (jv_get_kind(t) == JV_KIND_NULL &&
|
||||
(jv_get_kind(k) == JV_KIND_STRING || jv_get_kind(k) == JV_KIND_NUMBER)) {
|
||||
jv_free(t);
|
||||
jv_free(k);
|
||||
v = jv_null();
|
||||
} else {
|
||||
v = jv_invalid_with_msg(jv_string_fmt("Cannot index %s with %s",
|
||||
jv_kind_name(jv_get_kind(t)),
|
||||
jv_kind_name(jv_get_kind(k))));
|
||||
jv_free(t);
|
||||
jv_free(k);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
static jv jv_modify(jv t, jv k, jv v) {
|
||||
int isnull = jv_get_kind(t) == JV_KIND_NULL;
|
||||
if (jv_get_kind(k) == JV_KIND_STRING &&
|
||||
(jv_get_kind(t) == JV_KIND_OBJECT || isnull)) {
|
||||
if (isnull) t = jv_object();
|
||||
t = jv_object_set(t, k, v);
|
||||
} else if (jv_get_kind(k) == JV_KIND_NUMBER &&
|
||||
(jv_get_kind(t) == JV_KIND_ARRAY || isnull)) {
|
||||
if (isnull) t = jv_array();
|
||||
t = jv_array_set(t, (int)jv_number_value(k), v);
|
||||
} else {
|
||||
jv err = jv_invalid_with_msg(jv_string_fmt("Cannot update field at %s index of %s",
|
||||
jv_kind_name(jv_get_kind(t)),
|
||||
jv_kind_name(jv_get_kind(v))));
|
||||
jv_free(t);
|
||||
jv_free(k);
|
||||
jv_free(v);
|
||||
t = err;
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
static jv jv_insert(jv root, jv value, jv* path, int pathlen) {
|
||||
if (pathlen == 0) {
|
||||
jv_free(root);
|
||||
return value;
|
||||
}
|
||||
return jv_modify(root, jv_copy(*path),
|
||||
jv_insert(jv_lookup(jv_copy(root), jv_copy(*path)), value, path+1, pathlen-1));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
|
||||
true/false/null:
|
||||
check kind
|
||||
|
||||
number:
|
||||
introduce/eliminate jv
|
||||
to integer
|
||||
|
||||
array:
|
||||
copy
|
||||
free
|
||||
slice
|
||||
index
|
||||
update
|
||||
|
||||
updateslice?
|
||||
|
||||
|
||||
*/
|
||||
@@ -0,0 +1,22 @@
|
||||
#ifndef JV_DTOA_H
|
||||
#define JV_DTOA_H
|
||||
#define Kmax 7
|
||||
|
||||
struct Bigint;
|
||||
struct dtoa_context {
|
||||
struct Bigint *freelist[Kmax+1];
|
||||
struct Bigint *p5s;
|
||||
};
|
||||
|
||||
void jvp_dtoa_context_init(struct dtoa_context* ctx);
|
||||
void jvp_dtoa_context_free(struct dtoa_context* ctx);
|
||||
|
||||
double jvp_strtod(struct dtoa_context* C, const char* s, char** se);
|
||||
|
||||
|
||||
char* jvp_dtoa(struct dtoa_context* C, double dd, int mode, int ndigits, int *decpt, int *sign, char **rve);
|
||||
void jvp_freedtoa(struct dtoa_context* C, char *s);
|
||||
|
||||
#define JVP_DTOA_FMT_MAX_LEN 32
|
||||
char* jvp_dtoa_fmt(struct dtoa_context* C, register char *b, double x);
|
||||
#endif
|
||||
+418
@@ -0,0 +1,418 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "jv.h"
|
||||
#include "jv_dtoa.h"
|
||||
#include "jv_parse.h"
|
||||
#include "jv_unicode.h"
|
||||
|
||||
typedef const char* presult;
|
||||
|
||||
#define TRY(x) do {presult msg__ = (x); if (msg__) return msg__; } while(0)
|
||||
#ifdef __GNUC__
|
||||
#define pfunc __attribute__((warn_unused_result)) presult
|
||||
#else
|
||||
#define pfunc presult
|
||||
#endif
|
||||
|
||||
void jv_parser_init(struct jv_parser* p) {
|
||||
p->stack = 0;
|
||||
p->stacklen = p->stackpos = 0;
|
||||
p->next = jv_invalid();
|
||||
p->tokenbuf = 0;
|
||||
p->tokenlen = p->tokenpos = 0;
|
||||
p->st = JV_PARSER_NORMAL;
|
||||
p->curr_buf = 0;
|
||||
p->curr_buf_length = p->curr_buf_pos = p->curr_buf_is_partial = 0;
|
||||
jvp_dtoa_context_init(&p->dtoa);
|
||||
}
|
||||
|
||||
void jv_parser_free(struct jv_parser* p) {
|
||||
jv_free(p->next);
|
||||
for (int i=0; i<p->stackpos; i++)
|
||||
jv_free(p->stack[i]);
|
||||
free(p->stack);
|
||||
free(p->tokenbuf);
|
||||
jvp_dtoa_context_free(&p->dtoa);
|
||||
}
|
||||
|
||||
static pfunc value(struct jv_parser* p, jv val) {
|
||||
if (jv_is_valid(p->next)) return "Expected separator between values";
|
||||
jv_free(p->next);
|
||||
p->next = val;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void push(struct jv_parser* p, jv v) {
|
||||
assert(p->stackpos <= p->stacklen);
|
||||
if (p->stackpos == p->stacklen) {
|
||||
p->stacklen = p->stacklen * 2 + 10;
|
||||
p->stack = realloc(p->stack, p->stacklen * sizeof(jv));
|
||||
}
|
||||
assert(p->stackpos < p->stacklen);
|
||||
p->stack[p->stackpos++] = v;
|
||||
}
|
||||
|
||||
static pfunc token(struct jv_parser* p, char ch) {
|
||||
switch (ch) {
|
||||
case '[':
|
||||
if (jv_is_valid(p->next)) return "Expected separator between values";
|
||||
push(p, jv_array());
|
||||
break;
|
||||
|
||||
case '{':
|
||||
if (jv_is_valid(p->next)) return "Expected separator between values";
|
||||
push(p, jv_object());
|
||||
break;
|
||||
|
||||
case ':':
|
||||
if (!jv_is_valid(p->next))
|
||||
return "Expected string key before ':'";
|
||||
if (p->stackpos == 0 || jv_get_kind(p->stack[p->stackpos-1]) != JV_KIND_OBJECT)
|
||||
return "':' not as part of an object";
|
||||
if (jv_get_kind(p->next) != JV_KIND_STRING)
|
||||
return "Object keys must be strings";
|
||||
push(p, p->next);
|
||||
p->next = jv_invalid();
|
||||
break;
|
||||
|
||||
case ',':
|
||||
if (!jv_is_valid(p->next))
|
||||
return "Expected value before ','";
|
||||
if (p->stackpos == 0)
|
||||
return "',' not as part of an object or array";
|
||||
if (jv_get_kind(p->stack[p->stackpos-1]) == JV_KIND_ARRAY) {
|
||||
p->stack[p->stackpos-1] = jv_array_append(p->stack[p->stackpos-1], p->next);
|
||||
p->next = jv_invalid();
|
||||
} else if (jv_get_kind(p->stack[p->stackpos-1]) == JV_KIND_STRING) {
|
||||
assert(p->stackpos > 1 && jv_get_kind(p->stack[p->stackpos-2]) == JV_KIND_OBJECT);
|
||||
p->stack[p->stackpos-2] = jv_object_set(p->stack[p->stackpos-2],
|
||||
p->stack[p->stackpos-1], p->next);
|
||||
p->stackpos--;
|
||||
p->next = jv_invalid();
|
||||
} else {
|
||||
// this case hits on input like {"a", "b"}
|
||||
return "Objects must consist of key:value pairs";
|
||||
}
|
||||
break;
|
||||
|
||||
case ']':
|
||||
if (p->stackpos == 0 || jv_get_kind(p->stack[p->stackpos-1]) != JV_KIND_ARRAY)
|
||||
return "Unmatched ']'";
|
||||
if (jv_is_valid(p->next)) {
|
||||
p->stack[p->stackpos-1] = jv_array_append(p->stack[p->stackpos-1], p->next);
|
||||
p->next = jv_invalid();
|
||||
} else {
|
||||
if (jv_array_length(jv_copy(p->stack[p->stackpos-1])) != 0) {
|
||||
// this case hits on input like [1,2,3,]
|
||||
return "Expected another array element";
|
||||
}
|
||||
}
|
||||
jv_free(p->next);
|
||||
p->next = p->stack[--p->stackpos];
|
||||
break;
|
||||
|
||||
case '}':
|
||||
if (p->stackpos == 0)
|
||||
return "Unmatched '}'";
|
||||
if (jv_is_valid(p->next)) {
|
||||
if (jv_get_kind(p->stack[p->stackpos-1]) != JV_KIND_STRING)
|
||||
return "Objects must consist of key:value pairs";
|
||||
assert(p->stackpos > 1 && jv_get_kind(p->stack[p->stackpos-2]) == JV_KIND_OBJECT);
|
||||
p->stack[p->stackpos-2] = jv_object_set(p->stack[p->stackpos-2],
|
||||
p->stack[p->stackpos-1], p->next);
|
||||
p->stackpos--;
|
||||
p->next = jv_invalid();
|
||||
} else {
|
||||
if (jv_get_kind(p->stack[p->stackpos-1]) != JV_KIND_OBJECT)
|
||||
return "Unmatched '}'";
|
||||
if (jv_object_length(jv_copy(p->stack[p->stackpos-1])) != 0)
|
||||
return "Expected another key-value pair";
|
||||
}
|
||||
jv_free(p->next);
|
||||
p->next = p->stack[--p->stackpos];
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static void tokenadd(struct jv_parser* p, char c) {
|
||||
assert(p->tokenpos <= p->tokenlen);
|
||||
if (p->tokenpos == p->tokenlen) {
|
||||
p->tokenlen = p->tokenlen*2 + 256;
|
||||
p->tokenbuf = realloc(p->tokenbuf, p->tokenlen);
|
||||
}
|
||||
assert(p->tokenpos < p->tokenlen);
|
||||
p->tokenbuf[p->tokenpos++] = c;
|
||||
}
|
||||
|
||||
static int unhex4(char* hex) {
|
||||
int r = 0;
|
||||
for (int i=0; i<4; i++) {
|
||||
char c = *hex++;
|
||||
int n;
|
||||
if ('0' <= c && c <= '9') n = c - '0';
|
||||
else if ('a' <= c && c <= 'f') n = c - 'a' + 10;
|
||||
else if ('A' <= c && c <= 'F') n = c - 'A' + 10;
|
||||
else return -1;
|
||||
r <<= 4;
|
||||
r |= n;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
static pfunc found_string(struct jv_parser* p) {
|
||||
char* in = p->tokenbuf;
|
||||
char* out = p->tokenbuf;
|
||||
char* end = p->tokenbuf + p->tokenpos;
|
||||
|
||||
while (in < end) {
|
||||
char c = *in++;
|
||||
if (c == '\\') {
|
||||
if (in >= end)
|
||||
return "Expected escape character at end of string";
|
||||
c = *in++;
|
||||
switch (c) {
|
||||
case '\\':
|
||||
case '"':
|
||||
case '/': *out++ = c; break;
|
||||
case 'b': *out++ = '\b'; break;
|
||||
case 'f': *out++ = '\f'; break;
|
||||
case 't': *out++ = '\t'; break;
|
||||
case 'n': *out++ = '\n'; break;
|
||||
case 'r': *out++ = '\r'; break;
|
||||
|
||||
case 'u':
|
||||
/* ahh, the complicated case */
|
||||
if (in + 4 > end)
|
||||
return "Invalid \\uXXXX escape";
|
||||
int hexvalue = unhex4(in);
|
||||
if (hexvalue < 0)
|
||||
return "Invalid characters in \\uXXXX escape";
|
||||
unsigned long codepoint = (unsigned long)hexvalue;
|
||||
in += 4;
|
||||
if (0xD800 <= codepoint && codepoint <= 0xDBFF) {
|
||||
/* who thought UTF-16 surrogate pairs were a good idea? */
|
||||
if (in + 6 > end || in[0] != '\\' || in[1] != 'u')
|
||||
return "Invalid \\uXXXX\\uXXXX surrogate pair escape";
|
||||
unsigned long surrogate = unhex4(in+2);
|
||||
if (!(0xDC00 <= surrogate && surrogate <= 0xDFFF))
|
||||
return "Invalid \\uXXXX\\uXXXX surrogate pair escape";
|
||||
in += 6;
|
||||
codepoint = 0x10000 + (((codepoint - 0xD800) << 10)
|
||||
|(surrogate - 0xDC00));
|
||||
}
|
||||
// FIXME assert valid codepoint
|
||||
out += jvp_utf8_encode(codepoint, out);
|
||||
break;
|
||||
|
||||
default:
|
||||
return "Invalid escape";
|
||||
}
|
||||
} else {
|
||||
*out++ = c;
|
||||
}
|
||||
}
|
||||
TRY(value(p, jv_string_sized(p->tokenbuf, out - p->tokenbuf)));
|
||||
p->tokenpos = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static pfunc check_literal(struct jv_parser* p) {
|
||||
if (p->tokenpos == 0) return 0;
|
||||
|
||||
const char* pattern = 0;
|
||||
int plen;
|
||||
jv v;
|
||||
switch (p->tokenbuf[0]) {
|
||||
case 't': pattern = "true"; plen = 4; v = jv_true(); break;
|
||||
case 'f': pattern = "false"; plen = 5; v = jv_false(); break;
|
||||
case 'n': pattern = "null"; plen = 4; v = jv_null(); break;
|
||||
}
|
||||
if (pattern) {
|
||||
if (p->tokenpos != plen) return "Invalid literal";
|
||||
for (int i=0; i<plen; i++)
|
||||
if (p->tokenbuf[i] != pattern[i])
|
||||
return "Invalid literal";
|
||||
TRY(value(p, v));
|
||||
} else {
|
||||
// FIXME: better parser
|
||||
p->tokenbuf[p->tokenpos] = 0; // FIXME: invalid
|
||||
char* end = 0;
|
||||
double d = jvp_strtod(&p->dtoa, p->tokenbuf, &end);
|
||||
if (end == 0 || *end != 0)
|
||||
return "Invalid numeric literal";
|
||||
TRY(value(p, jv_number(d)));
|
||||
}
|
||||
p->tokenpos = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
typedef enum {
|
||||
LITERAL,
|
||||
WHITESPACE,
|
||||
STRUCTURE,
|
||||
QUOTE,
|
||||
INVALID
|
||||
} chclass;
|
||||
|
||||
static chclass classify(char c) {
|
||||
switch (c) {
|
||||
case ' ':
|
||||
case '\t':
|
||||
case '\r':
|
||||
case '\n':
|
||||
return WHITESPACE;
|
||||
case '"':
|
||||
return QUOTE;
|
||||
case '[':
|
||||
case ',':
|
||||
case ']':
|
||||
case '{':
|
||||
case ':':
|
||||
case '}':
|
||||
return STRUCTURE;
|
||||
default:
|
||||
return LITERAL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static presult OK = "output produced";
|
||||
|
||||
static int check_done(struct jv_parser* p, jv* out) {
|
||||
if (p->stackpos == 0 && jv_is_valid(p->next)) {
|
||||
*out = p->next;
|
||||
p->next = jv_invalid();
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static pfunc scan(struct jv_parser* p, char ch, jv* out) {
|
||||
presult answer = 0;
|
||||
if (p->st == JV_PARSER_NORMAL) {
|
||||
chclass cls = classify(ch);
|
||||
if (cls != LITERAL) {
|
||||
TRY(check_literal(p));
|
||||
if (check_done(p, out)) answer = OK;
|
||||
}
|
||||
switch (cls) {
|
||||
case LITERAL:
|
||||
tokenadd(p, ch);
|
||||
break;
|
||||
case WHITESPACE:
|
||||
break;
|
||||
case QUOTE:
|
||||
p->st = JV_PARSER_STRING;
|
||||
break;
|
||||
case STRUCTURE:
|
||||
TRY(token(p, ch));
|
||||
break;
|
||||
case INVALID:
|
||||
return "Invalid character";
|
||||
}
|
||||
if (check_done(p, out)) answer = OK;
|
||||
} else {
|
||||
if (ch == '"' && p->st == JV_PARSER_STRING) {
|
||||
TRY(found_string(p));
|
||||
p->st = JV_PARSER_NORMAL;
|
||||
if (check_done(p, out)) answer = OK;
|
||||
} else {
|
||||
tokenadd(p, ch);
|
||||
if (ch == '\\' && p->st == JV_PARSER_STRING) {
|
||||
p->st = JV_PARSER_STRING_ESCAPE;
|
||||
} else {
|
||||
p->st = JV_PARSER_STRING;
|
||||
}
|
||||
}
|
||||
}
|
||||
return answer;
|
||||
}
|
||||
|
||||
void jv_parser_set_buf(struct jv_parser* p, const char* buf, int length, int is_partial) {
|
||||
assert((p->curr_buf == 0 || p->curr_buf_pos == p->curr_buf_length)
|
||||
&& "previous buffer not exhausted");
|
||||
p->curr_buf = buf;
|
||||
p->curr_buf_length = length;
|
||||
p->curr_buf_pos = 0;
|
||||
p->curr_buf_is_partial = is_partial;
|
||||
}
|
||||
|
||||
jv jv_parser_next(struct jv_parser* p) {
|
||||
assert(p->curr_buf && "a buffer must be provided");
|
||||
jv value;
|
||||
presult msg = 0;
|
||||
while (!msg && p->curr_buf_pos < p->curr_buf_length) {
|
||||
char ch = p->curr_buf[p->curr_buf_pos++];
|
||||
msg = scan(p, ch, &value);
|
||||
}
|
||||
if (msg == OK) {
|
||||
return value;
|
||||
} else if (msg) {
|
||||
return jv_invalid_with_msg(jv_string(msg));
|
||||
} else if (p->curr_buf_is_partial) {
|
||||
assert(p->curr_buf_pos == p->curr_buf_length);
|
||||
// need another buffer
|
||||
return jv_invalid();
|
||||
} else {
|
||||
assert(p->curr_buf_pos == p->curr_buf_length);
|
||||
// at EOF
|
||||
if (p->st != JV_PARSER_NORMAL)
|
||||
return jv_invalid_with_msg(jv_string("Unfinished string"));
|
||||
if ((msg = check_literal(p)))
|
||||
return jv_invalid_with_msg(jv_string(msg));
|
||||
if (p->stackpos != 0)
|
||||
return jv_invalid_with_msg(jv_string("Unfinished JSON term"));
|
||||
// p->next is either invalid (nothing here but no syntax error)
|
||||
// or valid (this is the value). either way it's the thing to return
|
||||
value = p->next;
|
||||
p->next = jv_invalid();
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
jv jv_parse_sized(const char* string, int length) {
|
||||
struct jv_parser parser;
|
||||
jv_parser_init(&parser);
|
||||
jv_parser_set_buf(&parser, string, length, 0);
|
||||
jv value = jv_parser_next(&parser);
|
||||
if (jv_is_valid(value)) {
|
||||
jv next = jv_parser_next(&parser);
|
||||
if (jv_is_valid(next)) {
|
||||
// multiple JSON values, we only wanted one
|
||||
jv_free(value);
|
||||
jv_free(next);
|
||||
value = jv_invalid_with_msg(jv_string("Unexpected extra JSON values"));
|
||||
} else if (jv_invalid_has_msg(jv_copy(next))) {
|
||||
// parser error after the first JSON value
|
||||
jv_free(value);
|
||||
value = next;
|
||||
} else {
|
||||
// a single valid JSON value
|
||||
jv_free(next);
|
||||
}
|
||||
} else if (jv_invalid_has_msg(jv_copy(value))) {
|
||||
// parse error, we'll return it
|
||||
} else {
|
||||
// no value at all
|
||||
jv_free(value);
|
||||
value = jv_invalid_with_msg(jv_string("Expected JSON value"));
|
||||
}
|
||||
jv_parser_free(&parser);
|
||||
|
||||
if (!jv_is_valid(value) && jv_invalid_has_msg(jv_copy(value))) {
|
||||
jv msg = jv_invalid_get_msg(value);
|
||||
value = jv_invalid_with_msg(jv_string_fmt("%s (while parsing '%s')",
|
||||
jv_string_value(msg),
|
||||
string));
|
||||
jv_free(msg);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
jv jv_parse(const char* string) {
|
||||
return jv_parse_sized(string, strlen(string));
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
#ifndef JV_PARSE_H
|
||||
#define JV_PARSE_H
|
||||
#include "jv_dtoa.h"
|
||||
struct jv_parser {
|
||||
const char* curr_buf;
|
||||
int curr_buf_length;
|
||||
int curr_buf_pos;
|
||||
int curr_buf_is_partial;
|
||||
|
||||
jv* stack;
|
||||
int stackpos;
|
||||
int stacklen;
|
||||
jv next;
|
||||
|
||||
char* tokenbuf;
|
||||
int tokenpos;
|
||||
int tokenlen;
|
||||
|
||||
struct dtoa_context dtoa;
|
||||
|
||||
enum {
|
||||
JV_PARSER_NORMAL,
|
||||
JV_PARSER_STRING,
|
||||
JV_PARSER_STRING_ESCAPE
|
||||
} st;
|
||||
};
|
||||
|
||||
void jv_parser_init(struct jv_parser* p);
|
||||
void jv_parser_free(struct jv_parser* p);
|
||||
|
||||
void jv_parser_set_buf(struct jv_parser* p, const char* buf, int length, int is_partial);
|
||||
|
||||
jv jv_parser_next(struct jv_parser* p);
|
||||
#endif
|
||||
+208
@@ -0,0 +1,208 @@
|
||||
#include "jv.h"
|
||||
#include <stdio.h>
|
||||
#include <float.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "jv_dtoa.h"
|
||||
#include "jv_unicode.h"
|
||||
|
||||
static void put_buf(const char* s, int len, FILE* fout, jv* strout) {
|
||||
if (strout) {
|
||||
*strout = jv_string_append_buf(*strout, s, len);
|
||||
} else {
|
||||
fwrite(s, 1, len, fout);
|
||||
}
|
||||
}
|
||||
|
||||
static void put_char(char c, FILE* fout, jv* strout) {
|
||||
put_buf(&c, 1, fout, strout);
|
||||
}
|
||||
|
||||
static void put_str(const char* s, FILE* fout, jv* strout) {
|
||||
put_buf(s, strlen(s), fout, strout);
|
||||
}
|
||||
|
||||
static void put_space(int n, FILE* fout, jv* strout) {
|
||||
while (n--) {
|
||||
put_char(' ', fout, strout);
|
||||
}
|
||||
}
|
||||
|
||||
static void jvp_dump_string(jv str, int ascii_only, FILE* F, jv* S) {
|
||||
assert(jv_get_kind(str) == JV_KIND_STRING);
|
||||
const char* i = jv_string_value(str);
|
||||
const char* end = i + jv_string_length(jv_copy(str));
|
||||
const char* cstart;
|
||||
int c = 0;
|
||||
char buf[32];
|
||||
while ((i = jvp_utf8_next((cstart = i), end, &c))) {
|
||||
assert(c != -1);
|
||||
int unicode_escape = 0;
|
||||
if (0x20 <= c && c <= 0x7E) {
|
||||
// printable ASCII
|
||||
if (c == '"' || c == '\\') {
|
||||
put_char('\\', F, S);
|
||||
}
|
||||
put_char(c, F, S);
|
||||
} else if (c < 0x20 || c == 0x7F) {
|
||||
// ASCII control character
|
||||
switch (c) {
|
||||
case '\b':
|
||||
put_char('\\', F, S);
|
||||
put_char('b', F, S);
|
||||
break;
|
||||
case '\t':
|
||||
put_char('\\', F, S);
|
||||
put_char('t', F, S);
|
||||
break;
|
||||
case '\r':
|
||||
put_char('\\', F, S);
|
||||
put_char('r', F, S);
|
||||
break;
|
||||
case '\n':
|
||||
put_char('\\', F, S);
|
||||
put_char('n', F, S);
|
||||
break;
|
||||
case '\f':
|
||||
put_char('\\', F, S);
|
||||
put_char('f', F, S);
|
||||
break;
|
||||
default:
|
||||
unicode_escape = 1;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if (ascii_only) {
|
||||
unicode_escape = 1;
|
||||
} else {
|
||||
put_buf(cstart, i - cstart, F, S);
|
||||
}
|
||||
}
|
||||
if (unicode_escape) {
|
||||
if (c <= 0xffff) {
|
||||
sprintf(buf, "\\u%04x", c);
|
||||
} else {
|
||||
c -= 0x10000;
|
||||
sprintf(buf, "\\u%04x\\u%04x",
|
||||
0xD800 | ((c & 0xffc00) >> 10),
|
||||
0xDC00 | (c & 0x003ff));
|
||||
}
|
||||
put_str(buf, F, S);
|
||||
}
|
||||
}
|
||||
assert(c != -1);
|
||||
}
|
||||
|
||||
enum { INDENT = 2 };
|
||||
|
||||
static void jv_dump_term(struct dtoa_context* C, jv x, int flags, int indent, FILE* F, jv* S) {
|
||||
char buf[JVP_DTOA_FMT_MAX_LEN];
|
||||
switch (jv_get_kind(x)) {
|
||||
case JV_KIND_INVALID:
|
||||
assert(0 && "Invalid value");
|
||||
break;
|
||||
case JV_KIND_NULL:
|
||||
put_str("null", F, S);
|
||||
break;
|
||||
case JV_KIND_FALSE:
|
||||
put_str("false", F, S);
|
||||
break;
|
||||
case JV_KIND_TRUE:
|
||||
put_str("true", F, S);
|
||||
break;
|
||||
case JV_KIND_NUMBER: {
|
||||
double d = jv_number_value(x);
|
||||
if (d != d) {
|
||||
// JSON doesn't have NaN, so we'll render it as "null"
|
||||
put_str("null", F, S);
|
||||
} else {
|
||||
// Normalise infinities to something we can print in valid JSON
|
||||
if (d > DBL_MAX) d = DBL_MAX;
|
||||
if (d < -DBL_MAX) d = -DBL_MAX;
|
||||
put_str(jvp_dtoa_fmt(C, buf, d), F, S);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case JV_KIND_STRING:
|
||||
put_char('"', F, S);
|
||||
jvp_dump_string(x, flags & JV_PRINT_ASCII, F, S);
|
||||
put_char('"', F, S);
|
||||
break;
|
||||
case JV_KIND_ARRAY: {
|
||||
if (jv_array_length(jv_copy(x)) == 0) {
|
||||
put_str("[]", F, S);
|
||||
break;
|
||||
}
|
||||
put_str("[", F, S);
|
||||
if (flags & JV_PRINT_PRETTY) {
|
||||
put_char('\n', F, S);
|
||||
put_space(indent+INDENT, F, S);
|
||||
}
|
||||
for (int i=0; i<jv_array_length(jv_copy(x)); i++) {
|
||||
if (i!=0) {
|
||||
if (flags & JV_PRINT_PRETTY) {
|
||||
put_str(",\n", F, S);
|
||||
put_space(indent+INDENT, F, S);
|
||||
} else {
|
||||
put_str(", ", F, S);
|
||||
}
|
||||
}
|
||||
jv_dump_term(C, jv_array_get(jv_copy(x), i), flags, indent + INDENT, F, S);
|
||||
}
|
||||
if (flags & JV_PRINT_PRETTY) {
|
||||
put_char('\n', F, S);
|
||||
put_space(indent, F, S);
|
||||
}
|
||||
put_char(']', F, S);
|
||||
break;
|
||||
}
|
||||
case JV_KIND_OBJECT: {
|
||||
if (jv_object_length(jv_copy(x)) == 0) {
|
||||
put_str("{}", F, S);
|
||||
break;
|
||||
}
|
||||
put_char('{', F, S);
|
||||
if (flags & JV_PRINT_PRETTY) {
|
||||
put_char('\n', F, S);
|
||||
put_space(indent+INDENT, F, S);
|
||||
}
|
||||
int first = 1;
|
||||
jv_object_foreach(i, x) {
|
||||
if (!first) {
|
||||
if (flags & JV_PRINT_PRETTY){
|
||||
put_str(",\n", F, S);
|
||||
put_space(indent+INDENT, F, S);
|
||||
} else {
|
||||
put_str(", ", F, S);
|
||||
}
|
||||
}
|
||||
first = 0;
|
||||
jv_dump_term(C, jv_object_iter_key(x, i), flags, indent + INDENT, F, S);
|
||||
put_str(": ", F, S);
|
||||
jv_dump_term(C, jv_object_iter_value(x, i), flags, indent + INDENT, F, S);
|
||||
}
|
||||
if (flags & JV_PRINT_PRETTY) {
|
||||
put_char('\n', F, S);
|
||||
put_space(indent, F, S);
|
||||
}
|
||||
put_char('}', F, S);
|
||||
}
|
||||
}
|
||||
jv_free(x);
|
||||
}
|
||||
|
||||
void jv_dump(jv x, int flags) {
|
||||
struct dtoa_context C;
|
||||
jvp_dtoa_context_init(&C);
|
||||
jv_dump_term(&C, x, flags, 0, stdout, 0);
|
||||
jvp_dtoa_context_free(&C);
|
||||
}
|
||||
|
||||
jv jv_dump_string(jv x, int flags) {
|
||||
struct dtoa_context C;
|
||||
jvp_dtoa_context_init(&C);
|
||||
jv s = jv_string("");
|
||||
jv_dump_term(&C, x, flags, 0, 0, &s);
|
||||
jvp_dtoa_context_free(&C);
|
||||
return s;
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include "jv_unicode.h"
|
||||
#include "jv_utf8_tables.gen.h"
|
||||
|
||||
const char* jvp_utf8_next(const char* in, const char* end, int* codepoint) {
|
||||
if (in == end) {
|
||||
codepoint = 0;
|
||||
return 0;
|
||||
}
|
||||
unsigned char first = (unsigned char)in[0];
|
||||
int length = utf8_coding_length[first];
|
||||
if (length == 0 || length == UTF8_CONTINUATION_BYTE || in + length > end) {
|
||||
*codepoint = -1;
|
||||
return 0;
|
||||
}
|
||||
*codepoint = ((unsigned)in[0]) & utf8_coding_bits[first];
|
||||
for (int i=1; i<length; i++) {
|
||||
int ch = (unsigned char)in[i];
|
||||
if (utf8_coding_length[(unsigned char)in[i]] != UTF8_CONTINUATION_BYTE){
|
||||
*codepoint = -1;
|
||||
return 0;
|
||||
}
|
||||
*codepoint = (*codepoint << 6) | (ch & 0x3f);
|
||||
}
|
||||
return in + length;
|
||||
}
|
||||
|
||||
int jvp_utf8_verify(const char* in, const char* end) {
|
||||
int codepoint = 0;
|
||||
while ((in = jvp_utf8_next(in, end, &codepoint))) {
|
||||
if (codepoint == -1) return 0;
|
||||
}
|
||||
return codepoint != -1;
|
||||
}
|
||||
|
||||
int jvp_utf8_encode_length(int codepoint) {
|
||||
if (codepoint <= 0x7F) return 1;
|
||||
else if (codepoint <= 0x7FF) return 2;
|
||||
else if (codepoint <= 0xFFFF) return 3;
|
||||
else return 4;
|
||||
}
|
||||
|
||||
int jvp_utf8_encode(int codepoint, char* out) {
|
||||
assert(codepoint >= 0 && codepoint <= 0x10FFFF);
|
||||
char* start = out;
|
||||
if (codepoint <= 0x7F) {
|
||||
*out++ = codepoint;
|
||||
} else if (codepoint <= 0x7FF) {
|
||||
*out++ = 0xC0 + ((codepoint & 0x7C0) >> 6);
|
||||
*out++ = 0x80 + ((codepoint & 0x03F));
|
||||
} else if(codepoint <= 0xFFFF) {
|
||||
*out++ = 0xE0 + ((codepoint & 0xF000) >> 12);
|
||||
*out++ = 0x80 + ((codepoint & 0x0FC0) >> 6);
|
||||
*out++ = 0x80 + ((codepoint & 0x003F));
|
||||
} else {
|
||||
*out++ = 0xF0 + ((codepoint & 0x1C0000) >> 18);
|
||||
*out++ = 0x80 + ((codepoint & 0x03F000) >> 12);
|
||||
*out++ = 0x80 + ((codepoint & 0x000FC0) >> 6);
|
||||
*out++ = 0x80 + ((codepoint & 0x00003F));
|
||||
}
|
||||
assert(out - start == jvp_utf8_encode_length(codepoint));
|
||||
return out - start;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#ifndef JV_UNICODE_H
|
||||
#define JV_UNICODE_H
|
||||
|
||||
const char* jvp_utf8_next(const char* in, const char* end, int* codepoint);
|
||||
|
||||
|
||||
int jvp_utf8_decode_length(char startchar);
|
||||
|
||||
int jvp_utf8_encode_length(int codepoint);
|
||||
int jvp_utf8_encode(int codepoint, char* out);
|
||||
#endif
|
||||
@@ -0,0 +1,35 @@
|
||||
#define UTF8_CONTINUATION_BYTE ((unsigned char)255)
|
||||
static const unsigned char utf8_coding_length[] =
|
||||
{0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
||||
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
||||
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
||||
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
||||
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
||||
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
||||
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
||||
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0x00, 0x00, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
|
||||
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
|
||||
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
|
||||
0x04, 0x04, 0x04, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
|
||||
static const unsigned char utf8_coding_bits[] =
|
||||
{0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f,
|
||||
0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f,
|
||||
0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f,
|
||||
0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f,
|
||||
0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f,
|
||||
0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f,
|
||||
0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f,
|
||||
0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f,
|
||||
0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,
|
||||
0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,
|
||||
0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,
|
||||
0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f,
|
||||
0x00, 0x00, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f,
|
||||
0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f,
|
||||
0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f,
|
||||
0x07, 0x07, 0x07, 0x07, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
|
||||
+2296
File diff suppressed because it is too large.
Load diff
+362
@@ -0,0 +1,362 @@
|
||||
#ifndef jq_yyHEADER_H
|
||||
#define jq_yyHEADER_H 1
|
||||
#define jq_yyIN_HEADER 1
|
||||
|
||||
#line 6 "lexer.gen.h"
|
||||
|
||||
#line 8 "lexer.gen.h"
|
||||
|
||||
#define YY_INT_ALIGNED short int
|
||||
|
||||
/* A lexical scanner generated by flex */
|
||||
|
||||
#define FLEX_SCANNER
|
||||
#define YY_FLEX_MAJOR_VERSION 2
|
||||
#define YY_FLEX_MINOR_VERSION 5
|
||||
#define YY_FLEX_SUBMINOR_VERSION 35
|
||||
#if YY_FLEX_SUBMINOR_VERSION > 0
|
||||
#define FLEX_BETA
|
||||
#endif
|
||||
|
||||
/* First, we deal with platform-specific or compiler-specific issues. */
|
||||
|
||||
/* begin standard C headers. */
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/* end standard C headers. */
|
||||
|
||||
/* flex integer type definitions */
|
||||
|
||||
#ifndef FLEXINT_H
|
||||
#define FLEXINT_H
|
||||
|
||||
/* C99 systems have <inttypes.h>. Non-C99 systems may or may not. */
|
||||
|
||||
#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
|
||||
|
||||
/* C99 says to define __STDC_LIMIT_MACROS before including stdint.h,
|
||||
* if you want the limit (max/min) macros for int types.
|
||||
*/
|
||||
#ifndef __STDC_LIMIT_MACROS
|
||||
#define __STDC_LIMIT_MACROS 1
|
||||
#endif
|
||||
|
||||
#include <inttypes.h>
|
||||
typedef int8_t flex_int8_t;
|
||||
typedef uint8_t flex_uint8_t;
|
||||
typedef int16_t flex_int16_t;
|
||||
typedef uint16_t flex_uint16_t;
|
||||
typedef int32_t flex_int32_t;
|
||||
typedef uint32_t flex_uint32_t;
|
||||
#else
|
||||
typedef signed char flex_int8_t;
|
||||
typedef short int flex_int16_t;
|
||||
typedef int flex_int32_t;
|
||||
typedef unsigned char flex_uint8_t;
|
||||
typedef unsigned short int flex_uint16_t;
|
||||
typedef unsigned int flex_uint32_t;
|
||||
|
||||
/* Limits of integral types. */
|
||||
#ifndef INT8_MIN
|
||||
#define INT8_MIN (-128)
|
||||
#endif
|
||||
#ifndef INT16_MIN
|
||||
#define INT16_MIN (-32767-1)
|
||||
#endif
|
||||
#ifndef INT32_MIN
|
||||
#define INT32_MIN (-2147483647-1)
|
||||
#endif
|
||||
#ifndef INT8_MAX
|
||||
#define INT8_MAX (127)
|
||||
#endif
|
||||
#ifndef INT16_MAX
|
||||
#define INT16_MAX (32767)
|
||||
#endif
|
||||
#ifndef INT32_MAX
|
||||
#define INT32_MAX (2147483647)
|
||||
#endif
|
||||
#ifndef UINT8_MAX
|
||||
#define UINT8_MAX (255U)
|
||||
#endif
|
||||
#ifndef UINT16_MAX
|
||||
#define UINT16_MAX (65535U)
|
||||
#endif
|
||||
#ifndef UINT32_MAX
|
||||
#define UINT32_MAX (4294967295U)
|
||||
#endif
|
||||
|
||||
#endif /* ! C99 */
|
||||
|
||||
#endif /* ! FLEXINT_H */
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
/* The "const" storage-class-modifier is valid. */
|
||||
#define YY_USE_CONST
|
||||
|
||||
#else /* ! __cplusplus */
|
||||
|
||||
/* C99 requires __STDC__ to be defined as 1. */
|
||||
#if defined (__STDC__)
|
||||
|
||||
#define YY_USE_CONST
|
||||
|
||||
#endif /* defined (__STDC__) */
|
||||
#endif /* ! __cplusplus */
|
||||
|
||||
#ifdef YY_USE_CONST
|
||||
#define yyconst const
|
||||
#else
|
||||
#define yyconst
|
||||
#endif
|
||||
|
||||
/* An opaque pointer. */
|
||||
#ifndef YY_TYPEDEF_YY_SCANNER_T
|
||||
#define YY_TYPEDEF_YY_SCANNER_T
|
||||
typedef void* yyscan_t;
|
||||
#endif
|
||||
|
||||
/* For convenience, these vars (plus the bison vars far below)
|
||||
are macros in the reentrant scanner. */
|
||||
#define yyin yyg->yyin_r
|
||||
#define yyout yyg->yyout_r
|
||||
#define yyextra yyg->yyextra_r
|
||||
#define yyleng yyg->yyleng_r
|
||||
#define yytext yyg->yytext_r
|
||||
#define yylineno (YY_CURRENT_BUFFER_LVALUE->yy_bs_lineno)
|
||||
#define yycolumn (YY_CURRENT_BUFFER_LVALUE->yy_bs_column)
|
||||
#define yy_flex_debug yyg->yy_flex_debug_r
|
||||
|
||||
/* Size of default input buffer. */
|
||||
#ifndef YY_BUF_SIZE
|
||||
#ifdef __ia64__
|
||||
/* On IA-64, the buffer size is 16k, not 8k.
|
||||
* Moreover, YY_BUF_SIZE is 2*YY_READ_BUF_SIZE in the general case.
|
||||
* Ditto for the __ia64__ case accordingly.
|
||||
*/
|
||||
#define YY_BUF_SIZE 32768
|
||||
#else
|
||||
#define YY_BUF_SIZE 16384
|
||||
#endif /* __ia64__ */
|
||||
#endif
|
||||
|
||||
#ifndef YY_TYPEDEF_YY_BUFFER_STATE
|
||||
#define YY_TYPEDEF_YY_BUFFER_STATE
|
||||
typedef struct yy_buffer_state *YY_BUFFER_STATE;
|
||||
#endif
|
||||
|
||||
#ifndef YY_TYPEDEF_YY_SIZE_T
|
||||
#define YY_TYPEDEF_YY_SIZE_T
|
||||
typedef size_t yy_size_t;
|
||||
#endif
|
||||
|
||||
#ifndef YY_STRUCT_YY_BUFFER_STATE
|
||||
#define YY_STRUCT_YY_BUFFER_STATE
|
||||
struct yy_buffer_state
|
||||
{
|
||||
FILE *yy_input_file;
|
||||
|
||||
char *yy_ch_buf; /* input buffer */
|
||||
char *yy_buf_pos; /* current position in input buffer */
|
||||
|
||||
/* Size of input buffer in bytes, not including room for EOB
|
||||
* characters.
|
||||
*/
|
||||
yy_size_t yy_buf_size;
|
||||
|
||||
/* Number of characters read into yy_ch_buf, not including EOB
|
||||
* characters.
|
||||
*/
|
||||
int yy_n_chars;
|
||||
|
||||
/* Whether we "own" the buffer - i.e., we know we created it,
|
||||
* and can realloc() it to grow it, and should free() it to
|
||||
* delete it.
|
||||
*/
|
||||
int yy_is_our_buffer;
|
||||
|
||||
/* Whether this is an "interactive" input source; if so, and
|
||||
* if we're using stdio for input, then we want to use getc()
|
||||
* instead of fread(), to make sure we stop fetching input after
|
||||
* each newline.
|
||||
*/
|
||||
int yy_is_interactive;
|
||||
|
||||
/* Whether we're considered to be at the beginning of a line.
|
||||
* If so, '^' rules will be active on the next match, otherwise
|
||||
* not.
|
||||
*/
|
||||
int yy_at_bol;
|
||||
|
||||
int yy_bs_lineno; /**< The line count. */
|
||||
int yy_bs_column; /**< The column count. */
|
||||
|
||||
/* Whether to try to fill the input buffer when we reach the
|
||||
* end of it.
|
||||
*/
|
||||
int yy_fill_buffer;
|
||||
|
||||
int yy_buffer_status;
|
||||
|
||||
};
|
||||
#endif /* !YY_STRUCT_YY_BUFFER_STATE */
|
||||
|
||||
void jq_yyrestart (FILE *input_file ,yyscan_t yyscanner );
|
||||
void jq_yy_switch_to_buffer (YY_BUFFER_STATE new_buffer ,yyscan_t yyscanner );
|
||||
YY_BUFFER_STATE jq_yy_create_buffer (FILE *file,int size ,yyscan_t yyscanner );
|
||||
void jq_yy_delete_buffer (YY_BUFFER_STATE b ,yyscan_t yyscanner );
|
||||
void jq_yy_flush_buffer (YY_BUFFER_STATE b ,yyscan_t yyscanner );
|
||||
void jq_yypush_buffer_state (YY_BUFFER_STATE new_buffer ,yyscan_t yyscanner );
|
||||
void jq_yypop_buffer_state (yyscan_t yyscanner );
|
||||
|
||||
YY_BUFFER_STATE jq_yy_scan_buffer (char *base,yy_size_t size ,yyscan_t yyscanner );
|
||||
YY_BUFFER_STATE jq_yy_scan_string (yyconst char *yy_str ,yyscan_t yyscanner );
|
||||
YY_BUFFER_STATE jq_yy_scan_bytes (yyconst char *bytes,int len ,yyscan_t yyscanner );
|
||||
|
||||
void *jq_yyalloc (yy_size_t ,yyscan_t yyscanner );
|
||||
void *jq_yyrealloc (void *,yy_size_t ,yyscan_t yyscanner );
|
||||
void jq_yyfree (void * ,yyscan_t yyscanner );
|
||||
|
||||
/* Begin user sect3 */
|
||||
|
||||
#define jq_yywrap(n) 1
|
||||
#define YY_SKIP_YYWRAP
|
||||
|
||||
#define yytext_ptr yytext_r
|
||||
|
||||
#ifdef YY_HEADER_EXPORT_START_CONDITIONS
|
||||
#define INITIAL 0
|
||||
#define IN_PAREN 1
|
||||
#define IN_BRACKET 2
|
||||
#define IN_BRACE 3
|
||||
#define IN_QQINTERP 4
|
||||
#define IN_QQSTRING 5
|
||||
|
||||
#endif
|
||||
|
||||
#ifndef YY_NO_UNISTD_H
|
||||
/* Special case for "unistd.h", since it is non-ANSI. We include it way
|
||||
* down here because we want the user's section 1 to have been scanned first.
|
||||
* The user has a chance to override it with an option.
|
||||
*/
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#define YY_EXTRA_TYPE int
|
||||
|
||||
int jq_yylex_init (yyscan_t* scanner);
|
||||
|
||||
int jq_yylex_init_extra (YY_EXTRA_TYPE user_defined,yyscan_t* scanner);
|
||||
|
||||
/* Accessor methods to globals.
|
||||
These are made visible to non-reentrant scanners for convenience. */
|
||||
|
||||
int jq_yylex_destroy (yyscan_t yyscanner );
|
||||
|
||||
int jq_yyget_debug (yyscan_t yyscanner );
|
||||
|
||||
void jq_yyset_debug (int debug_flag ,yyscan_t yyscanner );
|
||||
|
||||
YY_EXTRA_TYPE jq_yyget_extra (yyscan_t yyscanner );
|
||||
|
||||
void jq_yyset_extra (YY_EXTRA_TYPE user_defined ,yyscan_t yyscanner );
|
||||
|
||||
FILE *jq_yyget_in (yyscan_t yyscanner );
|
||||
|
||||
void jq_yyset_in (FILE * in_str ,yyscan_t yyscanner );
|
||||
|
||||
FILE *jq_yyget_out (yyscan_t yyscanner );
|
||||
|
||||
void jq_yyset_out (FILE * out_str ,yyscan_t yyscanner );
|
||||
|
||||
int jq_yyget_leng (yyscan_t yyscanner );
|
||||
|
||||
char *jq_yyget_text (yyscan_t yyscanner );
|
||||
|
||||
int jq_yyget_lineno (yyscan_t yyscanner );
|
||||
|
||||
void jq_yyset_lineno (int line_number ,yyscan_t yyscanner );
|
||||
|
||||
YYSTYPE * jq_yyget_lval (yyscan_t yyscanner );
|
||||
|
||||
void jq_yyset_lval (YYSTYPE * yylval_param ,yyscan_t yyscanner );
|
||||
|
||||
YYLTYPE *jq_yyget_lloc (yyscan_t yyscanner );
|
||||
|
||||
void jq_yyset_lloc (YYLTYPE * yylloc_param ,yyscan_t yyscanner );
|
||||
|
||||
/* Macros after this point can all be overridden by user definitions in
|
||||
* section 1.
|
||||
*/
|
||||
|
||||
#ifndef YY_SKIP_YYWRAP
|
||||
#ifdef __cplusplus
|
||||
extern "C" int jq_yywrap (yyscan_t yyscanner );
|
||||
#else
|
||||
extern int jq_yywrap (yyscan_t yyscanner );
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef yytext_ptr
|
||||
static void yy_flex_strncpy (char *,yyconst char *,int ,yyscan_t yyscanner);
|
||||
#endif
|
||||
|
||||
#ifdef YY_NEED_STRLEN
|
||||
static int yy_flex_strlen (yyconst char * ,yyscan_t yyscanner);
|
||||
#endif
|
||||
|
||||
#ifndef YY_NO_INPUT
|
||||
|
||||
#endif
|
||||
|
||||
/* Amount of stuff to slurp up with each read. */
|
||||
#ifndef YY_READ_BUF_SIZE
|
||||
#ifdef __ia64__
|
||||
/* On IA-64, the buffer size is 16k, not 8k */
|
||||
#define YY_READ_BUF_SIZE 16384
|
||||
#else
|
||||
#define YY_READ_BUF_SIZE 8192
|
||||
#endif /* __ia64__ */
|
||||
#endif
|
||||
|
||||
/* Number of entries by which start-condition stack grows. */
|
||||
#ifndef YY_START_STACK_INCR
|
||||
#define YY_START_STACK_INCR 25
|
||||
#endif
|
||||
|
||||
/* Default declaration of generated scanner - a define so the user can
|
||||
* easily add parameters.
|
||||
*/
|
||||
#ifndef YY_DECL
|
||||
#define YY_DECL_IS_OURS 1
|
||||
|
||||
extern int jq_yylex \
|
||||
(YYSTYPE * yylval_param,YYLTYPE * yylloc_param ,yyscan_t yyscanner);
|
||||
|
||||
#define YY_DECL int jq_yylex \
|
||||
(YYSTYPE * yylval_param, YYLTYPE * yylloc_param , yyscan_t yyscanner)
|
||||
#endif /* !YY_DECL */
|
||||
|
||||
/* yy_get_previous_state - get the state just before the EOB char was reached */
|
||||
|
||||
#undef YY_NEW_FILE
|
||||
#undef YY_FLUSH_BUFFER
|
||||
#undef yy_set_bol
|
||||
#undef yy_new_buffer
|
||||
#undef yy_set_interactive
|
||||
#undef YY_DO_BEFORE_ACTION
|
||||
|
||||
#ifdef YY_DECL_IS_OURS
|
||||
#undef YY_DECL_IS_OURS
|
||||
#undef YY_DECL
|
||||
#endif
|
||||
|
||||
#line 104 "lexer.l"
|
||||
|
||||
|
||||
#line 361 "lexer.gen.h"
|
||||
#undef jq_yyIN_HEADER
|
||||
#endif /* jq_yyHEADER_H */
|
||||
@@ -0,0 +1,145 @@
|
||||
%{
|
||||
#include "compile.h"
|
||||
#include "parser.gen.h" /* Generated by bison. */
|
||||
|
||||
#define YY_USER_ACTION \
|
||||
do { \
|
||||
yylloc->start = yyget_extra(yyscanner); \
|
||||
yylloc->end = yylloc->start + yyleng; \
|
||||
yyset_extra(yylloc->end, yyscanner); \
|
||||
} while (0);
|
||||
|
||||
%}
|
||||
|
||||
%s IN_PAREN
|
||||
%s IN_BRACKET
|
||||
%s IN_BRACE
|
||||
%s IN_QQINTERP
|
||||
%x IN_QQSTRING
|
||||
%{
|
||||
static int enter(int opening, int state, yyscan_t yyscanner);
|
||||
static int try_exit(int closing, int state, yyscan_t yyscanner);
|
||||
%}
|
||||
|
||||
%option noyywrap nounput noinput nodefault
|
||||
%option reentrant
|
||||
%option extra-type="int"
|
||||
%option bison-bridge bison-locations
|
||||
%option prefix="jq_yy"
|
||||
%option stack
|
||||
|
||||
|
||||
%%
|
||||
|
||||
"==" { return EQ; }
|
||||
"as" { return AS; }
|
||||
"def" { return DEF; }
|
||||
"if" { return IF; }
|
||||
"then" { return THEN; }
|
||||
"else" { return ELSE; }
|
||||
"elif" { return ELSE_IF; }
|
||||
"and" { return AND; }
|
||||
"or" { return OR; }
|
||||
"end" { return END; }
|
||||
"//" { return DEFINEDOR; }
|
||||
"|=" { return SETPIPE; }
|
||||
"+=" { return SETPLUS; }
|
||||
"-=" { return SETMINUS; }
|
||||
"*=" { return SETMULT; }
|
||||
"/=" { return SETDIV; }
|
||||
"//=" { return SETDEFINEDOR; }
|
||||
"<=" { return LESSEQ; }
|
||||
">=" { return GREATEREQ; }
|
||||
"."|"="|";"|","|":"|"|"|"+"|"-"|"*"|"/"|"\$"|"<"|">" { return yytext[0];}
|
||||
|
||||
"["|"{"|"(" {
|
||||
return enter(yytext[0], YY_START, yyscanner);
|
||||
}
|
||||
|
||||
"]"|"}"|")" {
|
||||
return try_exit(yytext[0], YY_START, yyscanner);
|
||||
}
|
||||
|
||||
|
||||
-?[0-9.]+([eE][+-]?[0-9]+)? {
|
||||
yylval->literal = jv_parse_sized(yytext, yyleng); return LITERAL;
|
||||
}
|
||||
|
||||
"\"" {
|
||||
yy_push_state(IN_QQSTRING, yyscanner);
|
||||
return QQSTRING_START;
|
||||
}
|
||||
|
||||
<IN_QQSTRING>{
|
||||
"\\(" {
|
||||
return enter(QQSTRING_INTERP_START, YY_START, yyscanner);
|
||||
}
|
||||
"\"" {
|
||||
yy_pop_state(yyscanner);
|
||||
return QQSTRING_END;
|
||||
}
|
||||
(\\[^u(]|\\u[a-zA-Z0-9]{0,4})+ {
|
||||
/* pass escapes to the json parser */
|
||||
jv escapes = jv_string_fmt("\"%.*s\"", yyleng, yytext);
|
||||
yylval->literal = jv_parse_sized(jv_string_value(escapes), jv_string_length(jv_copy(escapes)));
|
||||
jv_free(escapes);
|
||||
return QQSTRING_TEXT;
|
||||
}
|
||||
[^\\\"]+ {
|
||||
yylval->literal = jv_string_sized(yytext, yyleng);
|
||||
return QQSTRING_TEXT;
|
||||
}
|
||||
. {
|
||||
return INVALID_CHARACTER;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[a-zA-Z_][a-zA-Z_0-9]* { yylval->literal = jv_string(yytext); return IDENT;}
|
||||
|
||||
[ \n\t]+ {}
|
||||
|
||||
. { return INVALID_CHARACTER; }
|
||||
|
||||
%%
|
||||
/* perhaps these should be calls... */
|
||||
/*
|
||||
"true" { return TRUE; }
|
||||
"false" { return FALSE; }
|
||||
"null" { return NULL; }
|
||||
*/
|
||||
static int try_exit(int c, int state, yyscan_t yyscanner) {
|
||||
char match = 0;
|
||||
int ret;
|
||||
switch (state) {
|
||||
case IN_PAREN: match = ret = ')'; break;
|
||||
case IN_BRACKET: match = ret = ']'; break;
|
||||
case IN_BRACE: match = ret = '}'; break;
|
||||
|
||||
case IN_QQINTERP:
|
||||
match = ')';
|
||||
ret = QQSTRING_INTERP_END;
|
||||
break;
|
||||
}
|
||||
assert(match);
|
||||
if (match == c) {
|
||||
yy_pop_state(yyscanner);
|
||||
return ret;
|
||||
} else {
|
||||
// FIXME: should we pop? Give a better error at least
|
||||
return INVALID_CHARACTER;
|
||||
}
|
||||
}
|
||||
|
||||
static int enter(int c, int currstate, yyscan_t yyscanner) {
|
||||
int state = 0;
|
||||
switch (c) {
|
||||
case '(': state = IN_PAREN; break;
|
||||
case '[': state = IN_BRACKET; break;
|
||||
case '{': state = IN_BRACE; break;
|
||||
case QQSTRING_INTERP_START: state = IN_QQINTERP; break;
|
||||
}
|
||||
assert(state);
|
||||
yy_push_state(state, yyscanner);
|
||||
return c;
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
#ifndef _LOCFILE_H
|
||||
#define _LOCFILE_H
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <stdarg.h>
|
||||
typedef struct {
|
||||
int start, end;
|
||||
} location;
|
||||
|
||||
static const location UNKNOWN_LOCATION = {-1, -1};
|
||||
|
||||
struct locfile {
|
||||
const char* data;
|
||||
int length;
|
||||
int* linemap;
|
||||
int nlines;
|
||||
};
|
||||
|
||||
static void locfile_init(struct locfile* l, const char* data, int length) {
|
||||
l->data = data;
|
||||
l->length = length;
|
||||
l->nlines = 1;
|
||||
for (int i=0; i<length; i++) {
|
||||
if (data[i] == '\n') l->nlines++;
|
||||
}
|
||||
l->linemap = malloc(sizeof(int) * (l->nlines + 1));
|
||||
l->linemap[0] = 0;
|
||||
int line = 1;
|
||||
for (int i=0; i<length; i++) {
|
||||
if (data[i] == '\n') {
|
||||
l->linemap[line] = i;
|
||||
line++;
|
||||
}
|
||||
}
|
||||
l->linemap[l->nlines] = length;
|
||||
}
|
||||
|
||||
static void locfile_free(struct locfile* l) {
|
||||
free(l->linemap);
|
||||
}
|
||||
|
||||
static int locfile_get_line(struct locfile* l, int pos) {
|
||||
assert(pos < l->length);
|
||||
int line = 0;
|
||||
while (l->linemap[line+1] < pos) line++;
|
||||
assert(line < l->nlines);
|
||||
return line;
|
||||
}
|
||||
|
||||
static int locfile_line_length(struct locfile* l, int line) {
|
||||
assert(line < l->nlines);
|
||||
return l->linemap[line+1] - l->linemap[line];
|
||||
}
|
||||
|
||||
static void locfile_locate(struct locfile* l, location loc, const char* fmt, ...) {
|
||||
va_list fmtargs;
|
||||
va_start(fmtargs, fmt);
|
||||
vfprintf(stderr, fmt, fmtargs);
|
||||
va_end(fmtargs);
|
||||
fprintf(stderr, "\n");
|
||||
if (loc.start == -1) {
|
||||
fprintf(stderr, "<unknown location>\n");
|
||||
return;
|
||||
}
|
||||
int startline = locfile_get_line(l, loc.start);
|
||||
int offset = l->linemap[startline];
|
||||
fprintf(stderr, "%.*s\n", locfile_line_length(l, startline), l->data + offset);
|
||||
fprintf(stderr, "%*s", loc.start - offset, "");
|
||||
for (int i = loc.start;
|
||||
i < loc.end && i < offset + locfile_line_length(l, startline);
|
||||
i++){
|
||||
fprintf(stderr, "^");
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,175 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include "compile.h"
|
||||
#include "builtin.h"
|
||||
#include "jv.h"
|
||||
#include "jv_parse.h"
|
||||
#include "locfile.h"
|
||||
#include "parser.h"
|
||||
#include "execute.h"
|
||||
#include "version.gen.h"
|
||||
|
||||
static const char* progname;
|
||||
|
||||
static void usage() {
|
||||
fprintf(stderr, "\njq - commandline JSON processor [version %s]\n", JQ_VERSION);
|
||||
fprintf(stderr, "Usage: %s [options] <jq filter>\n\n", progname);
|
||||
fprintf(stderr, "For a description of the command line options and\n");
|
||||
fprintf(stderr, "how to write jq filters (and why you might want to)\n");
|
||||
fprintf(stderr, "see the jq documentation at http://stedolan.github.com/jq\n\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
static void die() {
|
||||
fprintf(stderr, "Use %s --help for help with command-line options,\n", progname);
|
||||
fprintf(stderr, "or see the jq documentation at http://stedolan.github.com/jq\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
static int isoptish(const char* text) {
|
||||
return text[0] == '-' && (text[1] == '-' || isalpha(text[1]));
|
||||
}
|
||||
|
||||
static int isoption(const char* text, char shortopt, const char* longopt) {
|
||||
if (text[0] != '-') return 0;
|
||||
if (strlen(text) == 2 && text[1] == shortopt) return 1;
|
||||
if (text[1] == '-' && !strcmp(text+2, longopt)) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
enum {
|
||||
SLURP = 1,
|
||||
RAW_INPUT = 2,
|
||||
PROVIDE_NULL = 4,
|
||||
|
||||
RAW_OUTPUT = 8,
|
||||
COMPACT_OUTPUT = 16,
|
||||
ASCII_OUTPUT = 32,
|
||||
};
|
||||
static int options = 0;
|
||||
static struct bytecode* bc;
|
||||
|
||||
static void process(jv value) {
|
||||
jq_init(bc, value);
|
||||
jv result;
|
||||
while (jv_is_valid(result = jq_next())) {
|
||||
if ((options & RAW_OUTPUT) && jv_get_kind(result) == JV_KIND_STRING) {
|
||||
fwrite(jv_string_value(result), 1, jv_string_length(jv_copy(result)), stdout);
|
||||
} else {
|
||||
int dumpopts = 0;
|
||||
if (!(options & COMPACT_OUTPUT)) dumpopts |= JV_PRINT_PRETTY;
|
||||
if (options & ASCII_OUTPUT) dumpopts |= JV_PRINT_ASCII;
|
||||
jv_dump(result, dumpopts);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
jv_free(result);
|
||||
jq_teardown();
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc) progname = argv[0];
|
||||
|
||||
const char* program = 0;
|
||||
for (int i=1; i<argc; i++) {
|
||||
if (!isoptish(argv[i])) {
|
||||
if (program) usage();
|
||||
program = argv[i];
|
||||
} else if (isoption(argv[i], 's', "slurp")) {
|
||||
options |= SLURP;
|
||||
} else if (isoption(argv[i], 'r', "raw-output")) {
|
||||
options |= RAW_OUTPUT;
|
||||
} else if (isoption(argv[i], 'c', "compact-output")) {
|
||||
options |= COMPACT_OUTPUT;
|
||||
} else if (isoption(argv[i], 'a', "ascii-output")) {
|
||||
options |= ASCII_OUTPUT;
|
||||
} else if (isoption(argv[i], 'R', "raw-input")) {
|
||||
options |= RAW_INPUT;
|
||||
} else if (isoption(argv[i], 'n', "null-input")) {
|
||||
options |= PROVIDE_NULL;
|
||||
} else if (isoption(argv[i], 'h', "help")) {
|
||||
usage();
|
||||
} else if (isoption(argv[i], 'V', "version")) {
|
||||
fprintf(stderr, "jq version %s\n", JQ_VERSION);
|
||||
return 0;
|
||||
} else {
|
||||
fprintf(stderr, "%s: Unknown option %s\n", progname, argv[i]);
|
||||
die();
|
||||
}
|
||||
}
|
||||
if (!program) usage();
|
||||
|
||||
if ((options & PROVIDE_NULL) && (options & (RAW_INPUT | SLURP))) {
|
||||
fprintf(stderr, "%s: --null-input cannot be used with --raw-input or --slurp\n", program);
|
||||
die();
|
||||
}
|
||||
|
||||
bc = jq_compile(program);
|
||||
if (!bc) return 1;
|
||||
|
||||
#if JQ_DEBUG
|
||||
dump_disassembly(0, bc);
|
||||
printf("\n");
|
||||
#endif
|
||||
|
||||
if (options & PROVIDE_NULL) {
|
||||
process(jv_null());
|
||||
} else {
|
||||
jv slurped;
|
||||
if (options & SLURP) slurped = jv_invalid();
|
||||
int first = 1;
|
||||
struct jv_parser parser;
|
||||
jv_parser_init(&parser);
|
||||
while (!feof(stdin)) {
|
||||
char buf[4096];
|
||||
if (!fgets(buf, sizeof(buf), stdin)) buf[0] = 0;
|
||||
if (options & RAW_INPUT) {
|
||||
int len = strlen(buf);
|
||||
if (len > 0) {
|
||||
if (options & SLURP) {
|
||||
if (first) slurped = jv_string(buf);
|
||||
else slurped = jv_string_concat(slurped, jv_string(buf));
|
||||
} else {
|
||||
if (buf[len-1] == '\n') buf[len-1] = 0;
|
||||
process(jv_string(buf));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
jv_parser_set_buf(&parser, buf, strlen(buf), !feof(stdin));
|
||||
jv value;
|
||||
while (jv_is_valid((value = jv_parser_next(&parser)))) {
|
||||
if (options & SLURP) {
|
||||
if (first) slurped = jv_array();
|
||||
slurped = jv_array_append(slurped, value);
|
||||
} else {
|
||||
process(value);
|
||||
}
|
||||
}
|
||||
if (jv_invalid_has_msg(jv_copy(value))) {
|
||||
jv msg = jv_invalid_get_msg(value);
|
||||
fprintf(stderr, "parse error: %s\n", jv_string_value(msg));
|
||||
jv_free(msg);
|
||||
break;
|
||||
} else {
|
||||
jv_free(value);
|
||||
}
|
||||
}
|
||||
first = 0;
|
||||
}
|
||||
jv_parser_free(&parser);
|
||||
if (options & SLURP) {
|
||||
if (jv_is_valid(slurped)) {
|
||||
process(slurped);
|
||||
} else {
|
||||
jv_free(slurped);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bytecode_free(bc);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
#include "opcode.h"
|
||||
|
||||
// flags, length
|
||||
#define NONE 0, 1
|
||||
#define CONSTANT OP_HAS_CONSTANT, 2
|
||||
#define VARIABLE (OP_HAS_VARIABLE | OP_HAS_BINDING), 3
|
||||
#define BRANCH OP_HAS_BRANCH, 2
|
||||
#define CFUNC (OP_HAS_SYMBOL | OP_HAS_CFUNC | OP_HAS_VARIABLE_LENGTH_ARGLIST), 2
|
||||
#define UFUNC (OP_HAS_UFUNC | OP_HAS_VARIABLE_LENGTH_ARGLIST), 2
|
||||
#define CLOSURE_PARAM_IMM (OP_IS_CALL_PSEUDO | OP_HAS_BINDING), 0
|
||||
#define CLOSURE_CREATE_IMM (OP_HAS_BLOCK | OP_IS_CALL_PSEUDO | OP_HAS_BINDING), 0
|
||||
#define CLOSURE_CREATE_C_IMM (OP_IS_CALL_PSEUDO | OP_HAS_BINDING), 0
|
||||
#define CLOSURE_REF_IMM (OP_IS_CALL_PSEUDO | OP_HAS_BINDING), 2
|
||||
|
||||
#define OP(name, imm, in, out) \
|
||||
{name, #name, imm, in, out},
|
||||
|
||||
static const struct opcode_description opcode_descriptions[] = {
|
||||
#include "opcode_list.h"
|
||||
};
|
||||
|
||||
static const struct opcode_description invalid_opcode_description = {
|
||||
-1, "#INVALID", 0, 0, 0, 0
|
||||
};
|
||||
|
||||
|
||||
const struct opcode_description* opcode_describe(opcode op) {
|
||||
if ((int)op >= 0 && (int)op < NUM_OPCODES) {
|
||||
return &opcode_descriptions[op];
|
||||
} else {
|
||||
return &invalid_opcode_description;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
#ifndef OPCODE_H
|
||||
#define OPCODE_H
|
||||
#include <assert.h>
|
||||
|
||||
typedef enum {
|
||||
#define OP(name, imm, in, out) name,
|
||||
#include "opcode_list.h"
|
||||
#undef OP
|
||||
} opcode;
|
||||
|
||||
enum {
|
||||
NUM_OPCODES =
|
||||
#define OP(name, imm, in, out) +1
|
||||
#include "opcode_list.h"
|
||||
#undef OP
|
||||
};
|
||||
|
||||
enum {
|
||||
OP_HAS_CONSTANT = 2,
|
||||
OP_HAS_VARIABLE = 4,
|
||||
OP_HAS_BRANCH = 8,
|
||||
OP_HAS_SYMBOL = 16,
|
||||
OP_HAS_CFUNC = 32,
|
||||
OP_HAS_UFUNC = 64,
|
||||
OP_IS_CALL_PSEUDO = 128,
|
||||
OP_HAS_VARIABLE_LENGTH_ARGLIST = 256,
|
||||
OP_HAS_BLOCK = 512,
|
||||
OP_HAS_BINDING = 1024,
|
||||
};
|
||||
struct opcode_description {
|
||||
opcode op;
|
||||
const char* name;
|
||||
|
||||
int flags;
|
||||
|
||||
// length in 16-bit units
|
||||
int length;
|
||||
|
||||
int stack_in, stack_out;
|
||||
};
|
||||
|
||||
const struct opcode_description* opcode_describe(opcode op);
|
||||
|
||||
static inline int opcode_length(opcode op) {
|
||||
return opcode_describe(op)->length;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,30 @@
|
||||
OP(LOADK, CONSTANT, 1, 1)
|
||||
OP(DUP, NONE, 1, 2)
|
||||
OP(SWAP, NONE, 2, 2)
|
||||
OP(POP, NONE, 1, 0)
|
||||
OP(LOADV, VARIABLE, 1, 1)
|
||||
OP(STOREV, VARIABLE, 1, 0)
|
||||
OP(INDEX, NONE, 2, 1)
|
||||
//OP(DISPLAY, NONE, 1, 0)
|
||||
OP(YIELD, NONE, 1, 0)
|
||||
OP(EACH, NONE, 1, 1)
|
||||
OP(FORK, BRANCH, 0, 0)
|
||||
OP(JUMP, BRANCH, 0, 0)
|
||||
OP(JUMP_F,BRANCH, 1, 0)
|
||||
OP(BACKTRACK, NONE, 0, 0)
|
||||
OP(APPEND, NONE, 2, 1)
|
||||
OP(INSERT, NONE, 4, 2)
|
||||
|
||||
OP(ASSIGN, VARIABLE, 3, 0)
|
||||
|
||||
OP(CALL_BUILTIN_1_1, CFUNC, 1, 1)
|
||||
OP(CALL_BUILTIN_2_1, CFUNC, 2, 1)
|
||||
OP(CALL_BUILTIN_3_1, CFUNC, 3, 1)
|
||||
|
||||
OP(CALL_1_1, UFUNC, 1, 1)
|
||||
OP(RET, NONE, 1, 1)
|
||||
|
||||
OP(CLOSURE_PARAM, CLOSURE_PARAM_IMM, 0, 0)
|
||||
OP(CLOSURE_REF, CLOSURE_REF_IMM, 0, 0)
|
||||
OP(CLOSURE_CREATE, CLOSURE_CREATE_IMM, 0, 0)
|
||||
OP(CLOSURE_CREATE_C, CLOSURE_CREATE_C_IMM, 0, 0)
|
||||
+2830
File diff suppressed because it is too large.
Load diff
+130
@@ -0,0 +1,130 @@
|
||||
/* A Bison parser, made by GNU Bison 2.5. */
|
||||
|
||||
/* Bison interface for Yacc-like parsers in C
|
||||
|
||||
Copyright (C) 1984, 1989-1990, 2000-2011 Free Software Foundation, Inc.
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
/* As a special exception, you may create a larger work that contains
|
||||
part or all of the Bison parser skeleton and distribute that work
|
||||
under terms of your choice, so long as that work isn't itself a
|
||||
parser generator using the skeleton or a modified version thereof
|
||||
as a parser skeleton. Alternatively, if you modify or redistribute
|
||||
the parser skeleton itself, you may (at your option) remove this
|
||||
special exception, which will cause the skeleton and the resulting
|
||||
Bison output files to be licensed under the GNU General Public
|
||||
License without this special exception.
|
||||
|
||||
This special exception was added by the Free Software Foundation in
|
||||
version 2.2 of Bison. */
|
||||
|
||||
/* "%code requires" blocks. */
|
||||
|
||||
/* Line 2068 of yacc.c */
|
||||
#line 6 "parser.y"
|
||||
|
||||
#include "locfile.h"
|
||||
#define YYLTYPE location
|
||||
#define YYLLOC_DEFAULT(Loc, Rhs, N) \
|
||||
do { \
|
||||
if (N) { \
|
||||
(Loc).start = YYRHSLOC(Rhs, 1).start; \
|
||||
(Loc).end = YYRHSLOC(Rhs, N).end; \
|
||||
} else { \
|
||||
(Loc).start = YYRHSLOC(Rhs, 0).end; \
|
||||
(Loc).end = YYRHSLOC(Rhs, 0).end; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
|
||||
|
||||
/* Line 2068 of yacc.c */
|
||||
#line 55 "parser.gen.h"
|
||||
|
||||
/* Tokens. */
|
||||
#ifndef YYTOKENTYPE
|
||||
# define YYTOKENTYPE
|
||||
/* Put the tokens into the symbol table, so that GDB and other debuggers
|
||||
know about them. */
|
||||
enum yytokentype {
|
||||
INVALID_CHARACTER = 258,
|
||||
IDENT = 259,
|
||||
LITERAL = 260,
|
||||
EQ = 261,
|
||||
DEFINEDOR = 262,
|
||||
AS = 263,
|
||||
DEF = 264,
|
||||
IF = 265,
|
||||
THEN = 266,
|
||||
ELSE = 267,
|
||||
ELSE_IF = 268,
|
||||
END = 269,
|
||||
AND = 270,
|
||||
OR = 271,
|
||||
SETPIPE = 272,
|
||||
SETPLUS = 273,
|
||||
SETMINUS = 274,
|
||||
SETMULT = 275,
|
||||
SETDIV = 276,
|
||||
SETDEFINEDOR = 277,
|
||||
LESSEQ = 278,
|
||||
GREATEREQ = 279,
|
||||
QQSTRING_START = 280,
|
||||
QQSTRING_TEXT = 281,
|
||||
QQSTRING_INTERP_START = 282,
|
||||
QQSTRING_INTERP_END = 283,
|
||||
QQSTRING_END = 284
|
||||
};
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
|
||||
typedef union YYSTYPE
|
||||
{
|
||||
|
||||
/* Line 2068 of yacc.c */
|
||||
#line 24 "parser.y"
|
||||
|
||||
jv literal;
|
||||
block blk;
|
||||
|
||||
|
||||
|
||||
/* Line 2068 of yacc.c */
|
||||
#line 108 "parser.gen.h"
|
||||
} YYSTYPE;
|
||||
# define YYSTYPE_IS_TRIVIAL 1
|
||||
# define yystype YYSTYPE /* obsolescent; will be withdrawn */
|
||||
# define YYSTYPE_IS_DECLARED 1
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if ! defined YYLTYPE && ! defined YYLTYPE_IS_DECLARED
|
||||
typedef struct YYLTYPE
|
||||
{
|
||||
int first_line;
|
||||
int first_column;
|
||||
int last_line;
|
||||
int last_column;
|
||||
} YYLTYPE;
|
||||
# define yyltype YYLTYPE /* obsolescent; will be withdrawn */
|
||||
# define YYLTYPE_IS_DECLARED 1
|
||||
# define YYLTYPE_IS_TRIVIAL 1
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
+3283
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,7 @@
|
||||
#ifndef PARSER_H
|
||||
#define PARSER_H
|
||||
|
||||
int jq_parse(struct locfile* source, block* answer);
|
||||
int jq_parse_library(struct locfile* locations, block* answer);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,462 @@
|
||||
%{
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "compile.h"
|
||||
%}
|
||||
%code requires {
|
||||
#include "locfile.h"
|
||||
#define YYLTYPE location
|
||||
#define YYLLOC_DEFAULT(Loc, Rhs, N) \
|
||||
do { \
|
||||
if (N) { \
|
||||
(Loc).start = YYRHSLOC(Rhs, 1).start; \
|
||||
(Loc).end = YYRHSLOC(Rhs, N).end; \
|
||||
} else { \
|
||||
(Loc).start = YYRHSLOC(Rhs, 0).end; \
|
||||
(Loc).end = YYRHSLOC(Rhs, 0).end; \
|
||||
} \
|
||||
} while (0)
|
||||
}
|
||||
|
||||
%locations
|
||||
%error-verbose
|
||||
%define api.pure
|
||||
%union {
|
||||
jv literal;
|
||||
block blk;
|
||||
}
|
||||
|
||||
%destructor { jv_free($$); } <literal>
|
||||
%destructor { block_free($$); } <blk>
|
||||
|
||||
%parse-param {block* answer}
|
||||
%parse-param {int* errors}
|
||||
%parse-param {struct locfile* locations}
|
||||
%parse-param {yyscan_t lexer}
|
||||
%lex-param {block* answer}
|
||||
%lex-param {int* errors}
|
||||
%lex-param {struct locfile* locations}
|
||||
%lex-param {yyscan_t lexer}
|
||||
|
||||
|
||||
%token INVALID_CHARACTER
|
||||
%token <literal> IDENT
|
||||
%token <literal> LITERAL
|
||||
%token EQ "=="
|
||||
%token DEFINEDOR "//"
|
||||
%token AS "as"
|
||||
%token DEF "def"
|
||||
%token IF "if"
|
||||
%token THEN "then"
|
||||
%token ELSE "else"
|
||||
%token ELSE_IF "elif"
|
||||
%token END "end"
|
||||
%token AND "and"
|
||||
%token OR "or"
|
||||
%token SETPIPE "|="
|
||||
%token SETPLUS "+="
|
||||
%token SETMINUS "-="
|
||||
%token SETMULT "*="
|
||||
%token SETDIV "/="
|
||||
%token SETDEFINEDOR "//="
|
||||
%token LESSEQ "<="
|
||||
%token GREATEREQ ">="
|
||||
|
||||
%token QQSTRING_START
|
||||
%token <literal> QQSTRING_TEXT
|
||||
%token QQSTRING_INTERP_START
|
||||
%token QQSTRING_INTERP_END
|
||||
%token QQSTRING_END
|
||||
|
||||
/* revolting hack */
|
||||
%left ';'
|
||||
%left '|'
|
||||
%left ','
|
||||
%right "//"
|
||||
%nonassoc '=' SETPIPE SETPLUS SETMINUS SETMULT SETDIV SETDEFINEDOR
|
||||
%left OR
|
||||
%left AND
|
||||
%nonassoc EQ '<' '>' LESSEQ GREATEREQ
|
||||
%left '+' '-'
|
||||
%left '*' '/'
|
||||
|
||||
|
||||
%type <blk> Exp Term MkDict MkDictPair ExpD ElseBody QQString FuncDef FuncDefs String
|
||||
%{
|
||||
#include "lexer.gen.h"
|
||||
#define FAIL(loc, msg) \
|
||||
do { \
|
||||
location l = loc; \
|
||||
yyerror(&l, answer, errors, locations, lexer, msg); \
|
||||
/*YYERROR*/; \
|
||||
} while (0)
|
||||
|
||||
void yyerror(YYLTYPE* loc, block* answer, int* errors,
|
||||
struct locfile* locations, yyscan_t lexer, const char *s){
|
||||
(*errors)++;
|
||||
locfile_locate(locations, *loc, "error: %s", s);
|
||||
}
|
||||
|
||||
int yylex(YYSTYPE* yylval, YYLTYPE* yylloc, block* answer, int* errors,
|
||||
struct locfile* locations, yyscan_t lexer) {
|
||||
while (1) {
|
||||
int tok = jq_yylex(yylval, yylloc, lexer);
|
||||
if (tok == INVALID_CHARACTER) {
|
||||
FAIL(*yylloc, "Invalid character");
|
||||
} else {
|
||||
if ((tok == LITERAL || tok == QQSTRING_TEXT) && !jv_is_valid(yylval->literal)) {
|
||||
jv msg = jv_invalid_get_msg(jv_copy(yylval->literal));
|
||||
if (jv_get_kind(msg) == JV_KIND_STRING) {
|
||||
FAIL(*yylloc, jv_string_value(msg));
|
||||
} else {
|
||||
FAIL(*yylloc, "Invalid literal");
|
||||
}
|
||||
jv_free(msg);
|
||||
jv_free(yylval->literal);
|
||||
yylval->literal = jv_null();
|
||||
}
|
||||
return tok;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static block gen_dictpair(block k, block v) {
|
||||
block b = gen_subexp(k);
|
||||
block_append(&b, gen_subexp(v));
|
||||
block_append(&b, gen_op_simple(INSERT));
|
||||
return b;
|
||||
}
|
||||
|
||||
static block gen_index(block obj, block key) {
|
||||
return block_join(obj, block_join(gen_subexp(key), gen_op_simple(INDEX)));
|
||||
}
|
||||
|
||||
static block gen_binop(block a, block b, int op) {
|
||||
const char* funcname = 0;
|
||||
switch (op) {
|
||||
case '+': funcname = "_plus"; break;
|
||||
case '-': funcname = "_minus"; break;
|
||||
case '*': funcname = "_multiply"; break;
|
||||
case '/': funcname = "_divide"; break;
|
||||
case EQ: funcname = "_equal"; break;
|
||||
case '<': funcname = "_less"; break;
|
||||
case '>': funcname = "_greater"; break;
|
||||
case LESSEQ: funcname = "_lesseq"; break;
|
||||
case GREATEREQ: funcname = "_greatereq"; break;
|
||||
}
|
||||
assert(funcname);
|
||||
|
||||
block c = gen_noop();
|
||||
block_append(&c, gen_subexp(a));
|
||||
block_append(&c, gen_subexp(b));
|
||||
block_append(&c, gen_op_call(CALL_1_1, gen_op_block_unbound(CLOSURE_REF, funcname)));
|
||||
return c;
|
||||
}
|
||||
|
||||
static block gen_format(block a) {
|
||||
return block_join(a, gen_op_call(CALL_1_1, gen_op_block_unbound(CLOSURE_REF, "tostring")));
|
||||
}
|
||||
|
||||
static block gen_update(block a, block op, int optype) {
|
||||
block assign = a;
|
||||
block_append(&assign, gen_op_simple(DUP));
|
||||
if (optype) {
|
||||
op = gen_binop(gen_noop(), op, optype);
|
||||
}
|
||||
block_append(&assign, op);
|
||||
return gen_assign(assign);
|
||||
}
|
||||
|
||||
%}
|
||||
|
||||
%%
|
||||
TopLevel:
|
||||
Exp {
|
||||
*answer = $1;
|
||||
} |
|
||||
FuncDefs {
|
||||
*answer = $1;
|
||||
}
|
||||
|
||||
FuncDefs:
|
||||
/* empty */ {
|
||||
$$ = gen_noop();
|
||||
} |
|
||||
FuncDef FuncDefs {
|
||||
$$ = block_join($1, $2);
|
||||
}
|
||||
|
||||
Exp:
|
||||
FuncDef Exp %prec ';' {
|
||||
$$ = block_bind($1, $2, OP_IS_CALL_PSEUDO);
|
||||
} |
|
||||
|
||||
Term "as" '$' IDENT '|' Exp {
|
||||
$$ = gen_op_simple(DUP);
|
||||
block_append(&$$, $1);
|
||||
block_append(&$$, block_bind(gen_op_var_unbound(STOREV, jv_string_value($4)), $6, OP_HAS_VARIABLE));
|
||||
jv_free($4);
|
||||
} |
|
||||
|
||||
"if" Exp "then" Exp ElseBody {
|
||||
$$ = gen_cond($2, $4, $5);
|
||||
} |
|
||||
"if" Exp error {
|
||||
FAIL(@$, "Possibly unterminated 'if' statment");
|
||||
$$ = $2;
|
||||
} |
|
||||
|
||||
Exp '=' Exp {
|
||||
block assign = gen_op_simple(DUP);
|
||||
block_append(&assign, $3);
|
||||
block_append(&assign, gen_op_simple(SWAP));
|
||||
block_append(&assign, $1);
|
||||
block_append(&assign, gen_op_simple(SWAP));
|
||||
$$ = gen_assign(assign);
|
||||
} |
|
||||
|
||||
Exp "or" Exp {
|
||||
$$ = gen_or($1, $3);
|
||||
} |
|
||||
|
||||
Exp "and" Exp {
|
||||
$$ = gen_and($1, $3);
|
||||
} |
|
||||
|
||||
Exp "//" Exp {
|
||||
$$ = gen_definedor($1, $3);
|
||||
} |
|
||||
|
||||
Exp "//=" Exp {
|
||||
$$ = gen_update($1, gen_definedor(gen_noop(), $3), 0);
|
||||
} |
|
||||
|
||||
Exp "|=" Exp {
|
||||
$$ = gen_update($1, $3, 0);
|
||||
} |
|
||||
|
||||
Exp '|' Exp {
|
||||
$$ = block_join($1, $3);
|
||||
} |
|
||||
|
||||
Exp ',' Exp {
|
||||
$$ = gen_both($1, $3);
|
||||
} |
|
||||
|
||||
Exp '+' Exp {
|
||||
$$ = gen_binop($1, $3, '+');
|
||||
} |
|
||||
|
||||
Exp "+=" Exp {
|
||||
$$ = gen_update($1, $3, '+');
|
||||
} |
|
||||
|
||||
Exp '-' Exp {
|
||||
$$ = gen_binop($1, $3, '-');
|
||||
} |
|
||||
|
||||
Exp "-=" Exp {
|
||||
$$ = gen_update($1, $3, '-');
|
||||
} |
|
||||
|
||||
Exp '*' Exp {
|
||||
$$ = gen_binop($1, $3, '*');
|
||||
} |
|
||||
|
||||
Exp "*=" Exp {
|
||||
$$ = gen_update($1, $3, '*');
|
||||
} |
|
||||
|
||||
Exp '/' Exp {
|
||||
$$ = gen_binop($1, $3, '/');
|
||||
} |
|
||||
|
||||
Exp "/=" Exp {
|
||||
$$ = gen_update($1, $3, '/');
|
||||
} |
|
||||
|
||||
Exp "==" Exp {
|
||||
$$ = gen_binop($1, $3, EQ);
|
||||
} |
|
||||
|
||||
Exp '<' Exp {
|
||||
$$ = gen_binop($1, $3, '<');
|
||||
} |
|
||||
|
||||
Exp '>' Exp {
|
||||
$$ = gen_binop($1, $3, '>');
|
||||
} |
|
||||
|
||||
Exp "<=" Exp {
|
||||
$$ = gen_binop($1, $3, LESSEQ);
|
||||
} |
|
||||
|
||||
Exp ">=" Exp {
|
||||
$$ = gen_binop($1, $3, GREATEREQ);
|
||||
} |
|
||||
|
||||
String {
|
||||
$$ = $1;
|
||||
} |
|
||||
|
||||
Term {
|
||||
$$ = $1;
|
||||
}
|
||||
|
||||
String:
|
||||
QQSTRING_START QQString QQSTRING_END {
|
||||
$$ = $2;
|
||||
}
|
||||
|
||||
FuncDef:
|
||||
"def" IDENT ':' Exp ';' {
|
||||
block body = block_join($4, gen_op_simple(RET));
|
||||
$$ = gen_op_block_defn_rec(CLOSURE_CREATE, jv_string_value($2), body);
|
||||
jv_free($2);
|
||||
} |
|
||||
|
||||
"def" IDENT '(' IDENT ')' ':' Exp ';' {
|
||||
block body = block_bind(gen_op_block_unbound(CLOSURE_PARAM, jv_string_value($4)), block_join($7, gen_op_simple(RET)), OP_IS_CALL_PSEUDO);
|
||||
$$ = gen_op_block_defn_rec(CLOSURE_CREATE, jv_string_value($2), body);
|
||||
jv_free($2);
|
||||
jv_free($4);
|
||||
}
|
||||
|
||||
QQString:
|
||||
/* empty */ {
|
||||
$$ = gen_op_const(LOADK, jv_string(""));
|
||||
} |
|
||||
QQString QQSTRING_TEXT {
|
||||
$$ = gen_binop($1, gen_op_const(LOADK, $2), '+');
|
||||
} |
|
||||
QQString QQSTRING_INTERP_START Exp QQSTRING_INTERP_END {
|
||||
$$ = gen_binop($1, gen_format($3), '+');
|
||||
}
|
||||
|
||||
|
||||
ElseBody:
|
||||
"elif" Exp "then" Exp ElseBody {
|
||||
$$ = gen_cond($2, $4, $5);
|
||||
} |
|
||||
"else" Exp "end" {
|
||||
$$ = $2;
|
||||
}
|
||||
|
||||
ExpD:
|
||||
ExpD '|' ExpD {
|
||||
$$ = block_join($1, $3);
|
||||
} |
|
||||
|
||||
Term {
|
||||
$$ = $1;
|
||||
}
|
||||
|
||||
|
||||
Term:
|
||||
'.' {
|
||||
$$ = gen_noop();
|
||||
} |
|
||||
Term '.' IDENT {
|
||||
$$ = gen_index($1, gen_op_const(LOADK, $3));
|
||||
} |
|
||||
'.' IDENT {
|
||||
$$ = gen_index(gen_noop(), gen_op_const(LOADK, $2));
|
||||
} |
|
||||
/* FIXME: string literals */
|
||||
Term '[' Exp ']' {
|
||||
$$ = gen_index($1, $3);
|
||||
} |
|
||||
Term '[' ']' {
|
||||
$$ = block_join($1, gen_op_simple(EACH));
|
||||
} |
|
||||
LITERAL {
|
||||
$$ = gen_op_const(LOADK, $1);
|
||||
} |
|
||||
'(' Exp ')' {
|
||||
$$ = $2;
|
||||
} |
|
||||
'[' Exp ']' {
|
||||
$$ = gen_collect($2);
|
||||
} |
|
||||
'[' ']' {
|
||||
$$ = gen_op_const(LOADK, jv_array());
|
||||
} |
|
||||
'{' MkDict '}' {
|
||||
$$ = gen_subexp(gen_op_const(LOADK, jv_object()));
|
||||
block_append(&$$, $2);
|
||||
block_append(&$$, gen_op_simple(POP));
|
||||
} |
|
||||
'$' IDENT {
|
||||
$$ = gen_location(@$, gen_op_var_unbound(LOADV, jv_string_value($2)));
|
||||
jv_free($2);
|
||||
} |
|
||||
IDENT {
|
||||
$$ = gen_location(@$, gen_op_call(CALL_1_1, gen_op_block_unbound(CLOSURE_REF, jv_string_value($1))));
|
||||
jv_free($1);
|
||||
} |
|
||||
IDENT '(' Exp ')' {
|
||||
$$ = gen_op_call(CALL_1_1,
|
||||
block_join(gen_op_block_unbound(CLOSURE_REF, jv_string_value($1)),
|
||||
block_bind(gen_op_block_defn(CLOSURE_CREATE,
|
||||
"lambda",
|
||||
block_join($3, gen_op_simple(RET))),
|
||||
gen_noop(), OP_IS_CALL_PSEUDO)));
|
||||
$$ = gen_location(@1, $$);
|
||||
jv_free($1);
|
||||
} |
|
||||
'(' error ')' { $$ = gen_noop(); } |
|
||||
'[' error ']' { $$ = gen_noop(); } |
|
||||
Term '[' error ']' { $$ = $1; } |
|
||||
'{' error '}' { $$ = gen_noop(); }
|
||||
|
||||
MkDict:
|
||||
{
|
||||
$$=gen_noop();
|
||||
} |
|
||||
MkDictPair { $$ = $1; }
|
||||
| MkDictPair ',' MkDict { $$=block_join($1, $3); }
|
||||
| error ',' MkDict { $$ = $3; }
|
||||
|
||||
MkDictPair
|
||||
: IDENT ':' ExpD {
|
||||
$$ = gen_dictpair(gen_op_const(LOADK, $1), $3);
|
||||
}
|
||||
| String ':' ExpD {
|
||||
$$ = gen_dictpair($1, $3);
|
||||
}
|
||||
| IDENT {
|
||||
$$ = gen_dictpair(gen_op_const(LOADK, jv_copy($1)),
|
||||
gen_index(gen_noop(), gen_op_const(LOADK, $1)));
|
||||
}
|
||||
| '(' Exp ')' ':' ExpD {
|
||||
$$ = gen_dictpair($2, $5);
|
||||
}
|
||||
| '(' error ')' ':' ExpD { $$ = $5; }
|
||||
%%
|
||||
|
||||
int jq_parse(struct locfile* locations, block* answer) {
|
||||
yyscan_t scanner;
|
||||
YY_BUFFER_STATE buf;
|
||||
jq_yylex_init_extra(0, &scanner);
|
||||
buf = jq_yy_scan_bytes(locations->data, locations->length, scanner);
|
||||
int errors = 0;
|
||||
*answer = gen_noop();
|
||||
yyparse(answer, &errors, locations, scanner);
|
||||
jq_yy_delete_buffer(buf, scanner);
|
||||
jq_yylex_destroy(scanner);
|
||||
if (errors > 0) {
|
||||
block_free(*answer);
|
||||
*answer = gen_noop();
|
||||
}
|
||||
return errors;
|
||||
}
|
||||
|
||||
int jq_parse_library(struct locfile* locations, block* answer) {
|
||||
int errs = jq_parse(locations, answer);
|
||||
if (errs) return errs;
|
||||
if (!block_has_only_binders(*answer, OP_IS_CALL_PSEUDO)) {
|
||||
locfile_locate(locations, UNKNOWN_LOCATION, "error: library should only have function definitions, not a main expression");
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,375 @@
|
||||
# Tests are groups of three lines: program, input, expected output
|
||||
# Blank lines and lines starting with # are ignored
|
||||
|
||||
#
|
||||
# Simple value tests to check parser. Input is irrelevant
|
||||
#
|
||||
|
||||
true
|
||||
null
|
||||
true
|
||||
|
||||
false
|
||||
null
|
||||
false
|
||||
|
||||
null
|
||||
42
|
||||
null
|
||||
|
||||
1
|
||||
null
|
||||
1
|
||||
|
||||
# FIXME: much more number testing needed
|
||||
|
||||
{}
|
||||
null
|
||||
{}
|
||||
|
||||
[]
|
||||
null
|
||||
[]
|
||||
|
||||
# We test escapes by matching them against Unicode codepoints
|
||||
# FIXME: more tests needed for weird unicode stuff (e.g. utf16 pairs)
|
||||
"Aa\r\n\t\b\f\u03bc"
|
||||
null
|
||||
"Aa\u000d\u000a\u0009\u0008\u000c\u03bc"
|
||||
|
||||
.
|
||||
"Aa\r\n\t\b\f\u03bc"
|
||||
"Aa\u000d\u000a\u0009\u0008\u000c\u03bc"
|
||||
|
||||
"inter\("pol" + "ation")"
|
||||
null
|
||||
"interpolation"
|
||||
|
||||
#
|
||||
# Dictionary construction syntax
|
||||
#
|
||||
|
||||
{a: 1}
|
||||
null
|
||||
{"a":1}
|
||||
|
||||
{a,b,(.d):.a,e:.b}
|
||||
{"a":1, "b":2, "c":3, "d":"c"}
|
||||
{"a":1, "b":2, "c":1, "e":2}
|
||||
|
||||
#
|
||||
# Field access, piping
|
||||
#
|
||||
|
||||
.foo
|
||||
{"foo": 42, "bar": 43}
|
||||
42
|
||||
|
||||
.foo | .bar
|
||||
{"foo": {"bar": 42}, "bar": "badvalue"}
|
||||
42
|
||||
|
||||
.foo.bar
|
||||
{"foo": {"bar": 42}, "bar": "badvalue"}
|
||||
42
|
||||
|
||||
.foo_bar
|
||||
{"foo_bar": 2}
|
||||
2
|
||||
|
||||
.["foo"].bar
|
||||
{"foo": {"bar": 42}, "bar": "badvalue"}
|
||||
42
|
||||
|
||||
|
||||
#
|
||||
# Multiple outputs, iteration
|
||||
#
|
||||
|
||||
.[]
|
||||
[1,2,3]
|
||||
1
|
||||
2
|
||||
3
|
||||
|
||||
1,1
|
||||
[]
|
||||
1
|
||||
1
|
||||
|
||||
[.]
|
||||
[2]
|
||||
[[2]]
|
||||
|
||||
[[2]]
|
||||
[3]
|
||||
[[2]]
|
||||
|
||||
[{}]
|
||||
[2]
|
||||
[{}]
|
||||
|
||||
[.[]]
|
||||
["a"]
|
||||
["a"]
|
||||
|
||||
[(.,1),((.,.[]),(2,3))]
|
||||
["a","b"]
|
||||
[["a","b"],1,["a","b"],"a","b",2,3]
|
||||
|
||||
[([5,5][]),.,.[]]
|
||||
[1,2,3]
|
||||
[5,5,[1,2,3],1,2,3]
|
||||
|
||||
{x: (1,2)},{x:3} | .x
|
||||
null
|
||||
1
|
||||
2
|
||||
3
|
||||
|
||||
#
|
||||
# Variables
|
||||
#
|
||||
|
||||
1 as $x | 2 as $y | [$x,$y,$x]
|
||||
null
|
||||
[1,2,1]
|
||||
|
||||
[1,2,3][] as $x | [[4,5,6,7][$x]]
|
||||
null
|
||||
[5]
|
||||
[6]
|
||||
[7]
|
||||
|
||||
1 as $x | [$x,$x,$x as $x | $x]
|
||||
null
|
||||
[1,1,1]
|
||||
|
||||
# [.,(.[] | {x:.},.),.,.[]]
|
||||
|
||||
#
|
||||
# Builtin functions
|
||||
#
|
||||
|
||||
1+1
|
||||
null
|
||||
2
|
||||
|
||||
1+1
|
||||
"wtasdf"
|
||||
2.0
|
||||
|
||||
1e+0+0.001e3
|
||||
"I wonder what this will be?"
|
||||
20e-1
|
||||
|
||||
.+4
|
||||
15
|
||||
19.0
|
||||
|
||||
[1,2,3] + [.]
|
||||
null
|
||||
[1,2,3,null]
|
||||
|
||||
{"a":1} + {"b":2} + {"c":3}
|
||||
"asdfasdf"
|
||||
{"a":1, "b":2, "c":3}
|
||||
|
||||
"asdf" + "jkl;" + . + . + .
|
||||
"some string"
|
||||
"asdfjkl;some stringsome stringsome string"
|
||||
|
||||
"\u0000\u0020\u0000" + .
|
||||
"\u0000\u0020\u0000"
|
||||
"\u0000 \u0000\u0000 \u0000"
|
||||
|
||||
42 - .
|
||||
11
|
||||
31
|
||||
|
||||
[1,2,3,4,1] - [.,3]
|
||||
1
|
||||
[2,4]
|
||||
|
||||
[10 * 20, 20 / .]
|
||||
4
|
||||
[200, 5]
|
||||
|
||||
1 + 2 * 2 + 10 / 2
|
||||
null
|
||||
10
|
||||
|
||||
[16 / 4 / 2, 16 / 4 * 2, 16 - 4 - 2, 16 - 4 + 2]
|
||||
null
|
||||
[2, 8, 10, 14]
|
||||
|
||||
1 + tonumber + ("10" | tonumber)
|
||||
4
|
||||
15
|
||||
|
||||
[{"a":42},.object,10,.num,false,true,null,"b",[1,4]] | .[] as $x | [$x == .[]]
|
||||
{"object": {"a":42}, "num":10.0}
|
||||
[true, true, false, false, false, false, false, false, false]
|
||||
[true, true, false, false, false, false, false, false, false]
|
||||
[false, false, true, true, false, false, false, false, false]
|
||||
[false, false, true, true, false, false, false, false, false]
|
||||
[false, false, false, false, true, false, false, false, false]
|
||||
[false, false, false, false, false, true, false, false, false]
|
||||
[false, false, false, false, false, false, true, false, false]
|
||||
[false, false, false, false, false, false, false, true, false]
|
||||
[false, false, false, false, false, false, false, false, true ]
|
||||
|
||||
[.[] | length]
|
||||
[[], {}, [1,2], {"a":42}, "asdf"]
|
||||
[0, 0, 2, 1, 4]
|
||||
|
||||
map(keys)
|
||||
[{}, {"abcd":1,"abc":2,"abcde":3}, {"x":1, "z": 3, "y":2}]
|
||||
[[], ["abc","abcd","abcde"], ["x","y","z"]]
|
||||
|
||||
[1,2,empty,3,empty,4]
|
||||
null
|
||||
[1,2,3,4]
|
||||
|
||||
map(add)
|
||||
[[], [1,2,3], ["a","b","c"], [[3],[4,5],[6]], [{"a":1}, {"b":2}, {"a":3}]]
|
||||
[null, 6, "abc", [3,4,5,6], {"a":3, "b": 2}]
|
||||
|
||||
#
|
||||
# User-defined functions
|
||||
# Oh god.
|
||||
#
|
||||
|
||||
def f: . + 1; def g: def g: . + 100; f | g | f; (f | g), g
|
||||
3.0
|
||||
106.0
|
||||
105.0
|
||||
|
||||
def f: (1000,2000); f
|
||||
123412345
|
||||
1000
|
||||
2000
|
||||
|
||||
([1,2] + [4,5])
|
||||
[1,2,3]
|
||||
[1,2,4,5]
|
||||
|
||||
true
|
||||
[1]
|
||||
true
|
||||
|
||||
null,1,null
|
||||
"hello"
|
||||
null
|
||||
1
|
||||
null
|
||||
|
||||
[1,2,3]
|
||||
[5,6]
|
||||
[1,2,3]
|
||||
|
||||
def f(x): x | x; f([.], . + [42])
|
||||
[1,2,3]
|
||||
[[[1,2,3]]]
|
||||
[[1,2,3],42]
|
||||
[[1,2,3,42]]
|
||||
[1,2,3,42,42]
|
||||
|
||||
# test closures and lexical scoping
|
||||
def id(x):x; 2000 as $x | def f(x):1 as $x | id([$x, x, x]); def g(x): 100 as $x | f($x,$x+x); g($x)
|
||||
"more testing"
|
||||
[1,100,2100.0,100,2100.0]
|
||||
|
||||
# test backtracking through function calls and returns
|
||||
# this test is *evil*
|
||||
[[20,10][1,0] as $x | def f: (100,200) as $y | def g: [$x + $y, .]; . + $x | g; f[0] | [f][0][1] | f]
|
||||
999999999
|
||||
[[110.0, 130.0], [210.0, 130.0], [110.0, 230.0], [210.0, 230.0], [120.0, 160.0], [220.0, 160.0], [120.0, 260.0], [220.0, 260.0]]
|
||||
|
||||
# test recursion
|
||||
def fac: if . == 1 then 1 else . * (. - 1 | fac) end; [.[] | fac]
|
||||
[1,2,3,4]
|
||||
[1,2,6,24]
|
||||
|
||||
|
||||
#
|
||||
# Assignment
|
||||
#
|
||||
.message = "goodbye"
|
||||
{"message": "hello"}
|
||||
{"message": "goodbye"}
|
||||
|
||||
.foo = .bar
|
||||
{"bar":42}
|
||||
{"foo":42, "bar":42}
|
||||
|
||||
.foo |= .+1
|
||||
{"foo": 42}
|
||||
{"foo": 43}
|
||||
|
||||
.[] += 2, .[] *= 2, .[] -= 2, .[] /= 2
|
||||
[1,3,5]
|
||||
[3,5,7]
|
||||
[2,6,10]
|
||||
[-1,1,3]
|
||||
[0.5, 1.5, 2.5]
|
||||
|
||||
.[0].a |= {"old":., "new":(.+1)}
|
||||
[{"a":1,"b":2}]
|
||||
[{"a":{"old":1, "new":2},"b":2}]
|
||||
|
||||
def inc(x): x |= .+1; inc(.[].a)
|
||||
[{"a":1,"b":2},{"a":2,"b":4},{"a":7,"b":8}]
|
||||
[{"a":2,"b":2},{"a":3,"b":4},{"a":8,"b":8}]
|
||||
|
||||
.[2][3] = 1
|
||||
[4]
|
||||
[4, null, [null, null, null, 1]]
|
||||
|
||||
.foo[2].bar = 1
|
||||
{"foo":[11], "bar":42}
|
||||
{"foo":[11,null,{"bar":1}], "bar":42}
|
||||
|
||||
#
|
||||
# Conditionals
|
||||
#
|
||||
|
||||
[.[] | if .foo then "yep" else "nope" end]
|
||||
[{"foo":0},{"foo":1},{"foo":[]},{"foo":true},{"foo":false},{"foo":null},{"foo":"foo"},{}]
|
||||
["yep","yep","yep","yep","nope","nope","yep","nope"]
|
||||
|
||||
[.[] | if .baz then "strange" elif .foo then "yep" else "nope" end]
|
||||
[{"foo":0},{"foo":1},{"foo":[]},{"foo":true},{"foo":false},{"foo":null},{"foo":"foo"},{}]
|
||||
["yep","yep","yep","yep","nope","nope","yep","nope"]
|
||||
|
||||
|
||||
# FIXME: define/test behaviour of 'if (.foo,.bar) then A else B end'
|
||||
|
||||
[.[] | [.foo[] // .bar]]
|
||||
[{"foo":[1,2], "bar": 42}, {"foo":[1], "bar": null}, {"foo":[null,false,3], "bar": 18}, {"foo":[], "bar":42}, {"foo": [null,false,null], "bar": 41}]
|
||||
[[1,2], [1], [3], [42], [41]]
|
||||
|
||||
# FIXME: behaviour of update operators
|
||||
# .[] //= .[0]
|
||||
# ["hello",true,false,[false],null]
|
||||
# ["hello",true,"hello",[false],"hello"]
|
||||
|
||||
.[] | [.[0] and .[1], .[0] or .[1]]
|
||||
[[true,[]], [false,1], [42,null], [null,false]]
|
||||
[true,true]
|
||||
[false,true]
|
||||
[false,true]
|
||||
[false,false]
|
||||
|
||||
[.[] | not]
|
||||
[1,0,false,null,true,"hello"]
|
||||
[false,false,true,true,false,false]
|
||||
|
||||
# Check numeric comparison binops
|
||||
[10 > 0, 10 > 10, 10 > 20, 10 < 0, 10 < 10, 10 < 20]
|
||||
{}
|
||||
[true,false,false,false,false,true]
|
||||
|
||||
[10 >= 0, 10 >= 10, 10 >= 20, 10 <= 0, 10 <= 10, 10 <= 20]
|
||||
{}
|
||||
[true,true,false,false,true,true]
|
||||
Reference in new issue
Block a user