Python: Add syntax example comments for document generation.

This commit is contained in:
Mark Shannon 2019-05-20 12:45:55 +01:00
Родитель 4f26b58a1a
Коммит a256945938
7 изменённых файлов: 125 добавлений и 0 удалений

Просмотреть файл

@ -3,6 +3,9 @@ import python
/** Syntactic node (Class, Function, Module, Expr, Stmt or Comprehension) corresponding to a flow node */ /** Syntactic node (Class, Function, Module, Expr, Stmt or Comprehension) corresponding to a flow node */
abstract class AstNode extends AstNode_ { abstract class AstNode extends AstNode_ {
/* Special comment for documentation generation */
/* syntax: */
/** Gets the scope that this node occurs in */ /** Gets the scope that this node occurs in */
abstract Scope getScope(); abstract Scope getScope();
@ -206,6 +209,8 @@ class ComprehensionList extends ComprehensionList_ {
/** A list of expressions */ /** A list of expressions */
class ExprList extends ExprList_ { class ExprList extends ExprList_ {
/* syntax: Expr, ... */
} }

Просмотреть файл

@ -63,6 +63,8 @@ class ClassExpr extends ClassExpr_ {
/** A class statement. Note that ClassDef extends Assign as a class definition binds the newly created class */ /** A class statement. Note that ClassDef extends Assign as a class definition binds the newly created class */
class ClassDef extends Assign { class ClassDef extends Assign {
/* syntax: class name(...): ... */
ClassDef() { ClassDef() {
/* This is an artificial assignment the rhs of which is a (possibly decorated) ClassExpr */ /* This is an artificial assignment the rhs of which is a (possibly decorated) ClassExpr */
exists(ClassExpr c | this.getValue() = c or this.getValue() = c.getADecoratorCall()) exists(ClassExpr c | this.getValue() = c or this.getValue() = c.getADecoratorCall())

Просмотреть файл

@ -131,6 +131,9 @@ class Expr extends Expr_, AstNode {
/** An attribute expression, such as `value.attr` */ /** An attribute expression, such as `value.attr` */
class Attribute extends Attribute_ { class Attribute extends Attribute_ {
/* Special comment for documentation generation */
/* syntax: Expr.name */
override Expr getASubExpression() { override Expr getASubExpression() {
result = this.getObject() result = this.getObject()
} }
@ -160,6 +163,8 @@ class Attribute extends Attribute_ {
/** A subscript expression, such as `value[slice]` */ /** A subscript expression, such as `value[slice]` */
class Subscript extends Subscript_ { class Subscript extends Subscript_ {
/* syntax: Expr[Expr] */
override Expr getASubExpression() { override Expr getASubExpression() {
result = this.getIndex() result = this.getIndex()
or or
@ -176,6 +181,8 @@ class Subscript extends Subscript_ {
/** A call expression, such as `func(...)` */ /** A call expression, such as `func(...)` */
class Call extends Call_ { class Call extends Call_ {
/* syntax: Expr(...) */
override Expr getASubExpression() { override Expr getASubExpression() {
result = this.getAPositionalArg() or result = this.getAPositionalArg() or
result = this.getAKeyword().getValue() or result = this.getAKeyword().getValue() or
@ -268,6 +275,8 @@ class Call extends Call_ {
/** A conditional expression such as, `body if test else orelse` */ /** A conditional expression such as, `body if test else orelse` */
class IfExp extends IfExp_ { class IfExp extends IfExp_ {
/* syntax: Expr if Expr else Expr */
override Expr getASubExpression() { override Expr getASubExpression() {
result = this.getTest() or result = this.getBody() or result = this.getOrelse() result = this.getTest() or result = this.getBody() or result = this.getOrelse()
} }
@ -278,6 +287,8 @@ class IfExp extends IfExp_ {
/** A starred expression, such as the `*rest` in the assignment `first, *rest = seq` */ /** A starred expression, such as the `*rest` in the assignment `first, *rest = seq` */
class Starred extends Starred_ { class Starred extends Starred_ {
/* syntax: *Expr */
override Expr getASubExpression() { override Expr getASubExpression() {
result = this.getValue() result = this.getValue()
} }
@ -288,6 +299,8 @@ class Starred extends Starred_ {
/** A yield expression, such as `yield value` */ /** A yield expression, such as `yield value` */
class Yield extends Yield_ { class Yield extends Yield_ {
/* syntax: yield Expr */
override Expr getASubExpression() { override Expr getASubExpression() {
result = this.getValue() result = this.getValue()
} }
@ -301,6 +314,8 @@ class Yield extends Yield_ {
/** A yield expression, such as `yield from value` */ /** A yield expression, such as `yield from value` */
class YieldFrom extends YieldFrom_ { class YieldFrom extends YieldFrom_ {
/* syntax: yield from Expr */
override Expr getASubExpression() { override Expr getASubExpression() {
result = this.getValue() result = this.getValue()
} }
@ -314,6 +329,8 @@ class YieldFrom extends YieldFrom_ {
/** A repr (backticks) expression, such as `` `value` `` */ /** A repr (backticks) expression, such as `` `value` `` */
class Repr extends Repr_ { class Repr extends Repr_ {
/* syntax: `Expr` */
override Expr getASubExpression() { override Expr getASubExpression() {
result = this.getValue() result = this.getValue()
} }
@ -330,6 +347,8 @@ class Repr extends Repr_ {
`"hello"` are treated as Bytes for Python2, but Unicode for Python3. */ `"hello"` are treated as Bytes for Python2, but Unicode for Python3. */
class Bytes extends StrConst { class Bytes extends StrConst {
/* syntax: b"hello" */
Bytes() { Bytes() {
not this.isUnicode() not this.isUnicode()
} }
@ -355,6 +374,8 @@ class Bytes extends StrConst {
/** An ellipsis expression, such as `...` */ /** An ellipsis expression, such as `...` */
class Ellipsis extends Ellipsis_ { class Ellipsis extends Ellipsis_ {
/* syntax: ... */
override Expr getASubExpression() { override Expr getASubExpression() {
none() none()
} }
@ -394,6 +415,8 @@ abstract class Num extends Num_, ImmutableLiteral {
/** An integer numeric constant, such as `7` or `0x9` */ /** An integer numeric constant, such as `7` or `0x9` */
class IntegerLiteral extends Num { class IntegerLiteral extends Num {
/* syntax: 4 */
IntegerLiteral() { IntegerLiteral() {
not this instanceof FloatLiteral and not this instanceof ImaginaryLiteral not this instanceof FloatLiteral and not this instanceof ImaginaryLiteral
} }
@ -425,6 +448,8 @@ class IntegerLiteral extends Num {
/** A floating point numeric constant, such as `0.4` or `4e3` */ /** A floating point numeric constant, such as `0.4` or `4e3` */
class FloatLiteral extends Num { class FloatLiteral extends Num {
/* syntax: 4.2 */
FloatLiteral() { FloatLiteral() {
not this instanceof ImaginaryLiteral and not this instanceof ImaginaryLiteral and
this.getN().regexpMatch(".*[.eE].*") this.getN().regexpMatch(".*[.eE].*")
@ -456,6 +481,9 @@ class FloatLiteral extends Num {
class ImaginaryLiteral extends Num { class ImaginaryLiteral extends Num {
private float value; private float value;
/* Special comment for documentation generation */
/* syntax: 1.0j */
ImaginaryLiteral() { ImaginaryLiteral() {
value = this.getN().regexpCapture("(.+)j.*", 1).toFloat() value = this.getN().regexpCapture("(.+)j.*", 1).toFloat()
} }
@ -513,6 +541,9 @@ class NegativeIntegerLiteral extends ImmutableLiteral, UnaryExpr {
"hello" are treated as Bytes for Python2, but Unicode for Python3. */ "hello" are treated as Bytes for Python2, but Unicode for Python3. */
class Unicode extends StrConst { class Unicode extends StrConst {
/* Special comment for documentation generation */
/* syntax: "hello" */
Unicode() { Unicode() {
this.isUnicode() this.isUnicode()
} }
@ -541,6 +572,9 @@ class Unicode extends StrConst {
/** A dictionary expression, such as `{'key':'value'}` */ /** A dictionary expression, such as `{'key':'value'}` */
class Dict extends Dict_ { class Dict extends Dict_ {
/* Special comment for documentation generation */
/* syntax: {Expr: Expr, ...} */
/** Gets the value of an item of this dict display */ /** Gets the value of an item of this dict display */
Expr getAValue() { Expr getAValue() {
result = this.getAnItem().(DictDisplayItem).getValue() result = this.getAnItem().(DictDisplayItem).getValue()
@ -566,6 +600,9 @@ class Dict extends Dict_ {
/** A list expression, such as `[ 1, 3, 5, 7, 9 ]` */ /** A list expression, such as `[ 1, 3, 5, 7, 9 ]` */
class List extends List_ { class List extends List_ {
/* Special comment for documentation generation */
/* syntax: [Expr, ...] */
override Expr getASubExpression() { override Expr getASubExpression() {
result = this.getAnElt() result = this.getAnElt()
} }
@ -575,6 +612,9 @@ class List extends List_ {
/** A set expression such as `{ 1, 3, 5, 7, 9 }` */ /** A set expression such as `{ 1, 3, 5, 7, 9 }` */
class Set extends Set_ { class Set extends Set_ {
/* Special comment for documentation generation */
/* syntax: {Expr, ...} */
override Expr getASubExpression() { override Expr getASubExpression() {
result = this.getAnElt() result = this.getAnElt()
} }
@ -601,6 +641,8 @@ class PlaceHolder extends PlaceHolder_ {
/** A tuple expression such as `( 1, 3, 5, 7, 9 )` */ /** A tuple expression such as `( 1, 3, 5, 7, 9 )` */
class Tuple extends Tuple_ { class Tuple extends Tuple_ {
/* syntax: (Expr, ...) */
override Expr getASubExpression() { override Expr getASubExpression() {
result = this.getAnElt() result = this.getAnElt()
} }
@ -612,6 +654,8 @@ class Tuple extends Tuple_ {
*/ */
class Name extends Name_ { class Name extends Name_ {
/* syntax: name */
string getId() { string getId() {
result = this.getVariable().getId() result = this.getVariable().getId()
} }
@ -705,6 +749,9 @@ class Slice extends Slice_ {
/** A string constant. */ /** A string constant. */
class StrConst extends Str_, ImmutableLiteral { class StrConst extends Str_, ImmutableLiteral {
/* Special comment for documentation generation */
/* syntax: "hello" */
predicate isUnicode() { predicate isUnicode() {
this.getPrefix().charAt(_) = "u" this.getPrefix().charAt(_) = "u"
or or
@ -790,6 +837,9 @@ abstract class BooleanLiteral extends NameConstant {
/** The boolean named constant `True` */ /** The boolean named constant `True` */
class True extends BooleanLiteral { class True extends BooleanLiteral {
/* Special comment for documentation generation */
/* syntax: True */
True() { True() {
name_consts(this, "True") name_consts(this, "True")
} }
@ -807,6 +857,9 @@ class True extends BooleanLiteral {
/** The boolean named constant `False` */ /** The boolean named constant `False` */
class False extends BooleanLiteral { class False extends BooleanLiteral {
/* Special comment for documentation generation */
/* syntax: False */
False() { False() {
name_consts(this, "False") name_consts(this, "False")
} }
@ -824,6 +877,9 @@ class False extends BooleanLiteral {
/** `None` */ /** `None` */
class None extends NameConstant { class None extends NameConstant {
/* Special comment for documentation generation */
/* syntax: None */
None() { None() {
name_consts(this, "None") name_consts(this, "None")
} }
@ -840,6 +896,9 @@ class None extends NameConstant {
/** An await expression such as `await coro`. */ /** An await expression such as `await coro`. */
class Await extends Await_ { class Await extends Await_ {
/* Special comment for documentation generation */
/* syntax: await Expr */
override Expr getASubExpression() { override Expr getASubExpression() {
result = this.getValue() result = this.getValue()
} }
@ -849,6 +908,8 @@ class Await extends Await_ {
/** A formatted string literal expression, such as `f'hello {world!s}'` */ /** A formatted string literal expression, such as `f'hello {world!s}'` */
class Fstring extends Fstring_ { class Fstring extends Fstring_ {
/* syntax: f"Yes!" */
override Expr getASubExpression() { override Expr getASubExpression() {
result = this.getAValue() result = this.getAValue()
} }

Просмотреть файл

@ -187,6 +187,8 @@ class Function extends Function_, Scope, AstNode {
/** A def statement. Note that FunctionDef extends Assign as a function definition binds the newly created function */ /** A def statement. Note that FunctionDef extends Assign as a function definition binds the newly created function */
class FunctionDef extends Assign { class FunctionDef extends Assign {
/* syntax: def name(...): ... */
FunctionDef() { FunctionDef() {
/* This is an artificial assignment the rhs of which is a (possibly decorated) FunctionExpr */ /* This is an artificial assignment the rhs of which is a (possibly decorated) FunctionExpr */
exists(FunctionExpr f | this.getValue() = f or this.getValue() = f.getADecoratorCall()) exists(FunctionExpr f | this.getValue() = f or this.getValue() = f.getADecoratorCall())

Просмотреть файл

@ -170,6 +170,8 @@ class ImportMember extends ImportMember_ {
/** An import statement */ /** An import statement */
class Import extends Import_ { class Import extends Import_ {
/* syntax: import modname */
private ImportExpr getAModuleExpr() { private ImportExpr getAModuleExpr() {
result = this.getAName().getValue() result = this.getAName().getValue()
or or
@ -222,6 +224,8 @@ class Import extends Import_ {
/** An import * statement */ /** An import * statement */
class ImportStar extends ImportStar_ { class ImportStar extends ImportStar_ {
/* syntax: from modname import * */
ImportExpr getModuleExpr() { ImportExpr getModuleExpr() {
result = this.getModule() result = this.getModule()
or or

Просмотреть файл

@ -2,6 +2,8 @@ import python
class KeyValuePair extends KeyValuePair_, DictDisplayItem { class KeyValuePair extends KeyValuePair_, DictDisplayItem {
/* syntax: Expr : Expr */
override Location getLocation() { override Location getLocation() {
result = KeyValuePair_.super.getLocation() result = KeyValuePair_.super.getLocation()
} }
@ -76,6 +78,8 @@ abstract class DictDisplayItem extends DictItem {
/** A keyword argument in a call. For example `arg=expr` in `foo(0, arg=expr)` */ /** A keyword argument in a call. For example `arg=expr` in `foo(0, arg=expr)` */
class Keyword extends Keyword_, DictUnpackingOrKeyword { class Keyword extends Keyword_, DictUnpackingOrKeyword {
/* syntax: name = Expr */
override Location getLocation() { override Location getLocation() {
result = Keyword_.super.getLocation() result = Keyword_.super.getLocation()
} }

Просмотреть файл

@ -98,6 +98,9 @@ class Assign extends Assign_ {
/** An assignment statement */ /** An assignment statement */
class AssignStmt extends Assign { class AssignStmt extends Assign {
/* Special comment for documentation generation */
/* syntax: Expr, ... = Expr */
AssignStmt() { AssignStmt() {
not this instanceof FunctionDef and not this instanceof ClassDef not this instanceof FunctionDef and not this instanceof ClassDef
} }
@ -110,6 +113,8 @@ class AssignStmt extends Assign {
/** An augmented assignment statement, such as `x += y` */ /** An augmented assignment statement, such as `x += y` */
class AugAssign extends AugAssign_ { class AugAssign extends AugAssign_ {
/* syntax: Expr += Expr */
override Expr getASubExpression() { override Expr getASubExpression() {
result = this.getOperation() result = this.getOperation()
} }
@ -130,6 +135,8 @@ class AugAssign extends AugAssign_ {
/** An annotated assignment statement, such as `x: int = 0` */ /** An annotated assignment statement, such as `x: int = 0` */
class AnnAssign extends AnnAssign_ { class AnnAssign extends AnnAssign_ {
/* syntax: Expr: Expr = Expr */
override Expr getASubExpression() { override Expr getASubExpression() {
result = this.getAnnotation() or result = this.getAnnotation() or
result = this.getTarget() or result = this.getTarget() or
@ -156,6 +163,8 @@ class AnnAssign extends AnnAssign_ {
/** An exec statement */ /** An exec statement */
class Exec extends Exec_ { class Exec extends Exec_ {
/* syntax: exec Expr */
override Expr getASubExpression() { override Expr getASubExpression() {
result = this.getBody() or result = this.getBody() or
result = this.getGlobals() or result = this.getGlobals() or
@ -171,6 +180,8 @@ class Exec extends Exec_ {
/** An except statement (part of a `try` statement), such as `except IOError as err:` */ /** An except statement (part of a `try` statement), such as `except IOError as err:` */
class ExceptStmt extends ExceptStmt_ { class ExceptStmt extends ExceptStmt_ {
/* syntax: except Expr [ as Expr ]: */
/** Gets the immediately enclosing try statement */ /** Gets the immediately enclosing try statement */
Try getTry() { Try getTry() {
result.getAHandler() = this result.getAHandler() = this
@ -195,6 +206,8 @@ class ExceptStmt extends ExceptStmt_ {
/** An assert statement, such as `assert a == b, "A is not equal to b"` */ /** An assert statement, such as `assert a == b, "A is not equal to b"` */
class Assert extends Assert_ { class Assert extends Assert_ {
/* syntax: assert Expr [, Expr] */
override Expr getASubExpression() { override Expr getASubExpression() {
result = this.getMsg() or result = this.getTest() result = this.getMsg() or result = this.getTest()
} }
@ -208,6 +221,8 @@ class Assert extends Assert_ {
/** A break statement */ /** A break statement */
class Break extends Break_ { class Break extends Break_ {
/* syntax: assert Expr [, Expr] */
override Expr getASubExpression() { override Expr getASubExpression() {
none() none()
} }
@ -221,6 +236,8 @@ class Break extends Break_ {
/** A continue statement */ /** A continue statement */
class Continue extends Continue_ { class Continue extends Continue_ {
/* syntax: continue */
override Expr getASubExpression() { override Expr getASubExpression() {
none() none()
} }
@ -234,6 +251,8 @@ class Continue extends Continue_ {
/** A delete statement, such as `del x[-1]` */ /** A delete statement, such as `del x[-1]` */
class Delete extends Delete_ { class Delete extends Delete_ {
/* syntax: del Expr, ... */
override Expr getASubExpression() { override Expr getASubExpression() {
result = this.getATarget() result = this.getATarget()
} }
@ -247,6 +266,8 @@ class Delete extends Delete_ {
/** An expression statement, such as `len(x)` or `yield y` */ /** An expression statement, such as `len(x)` or `yield y` */
class ExprStmt extends ExprStmt_ { class ExprStmt extends ExprStmt_ {
/* syntax: Expr */
override Expr getASubExpression() { override Expr getASubExpression() {
result = this.getValue() result = this.getValue()
} }
@ -260,6 +281,8 @@ class ExprStmt extends ExprStmt_ {
/** A for statement, such as `for x in y: print(x)` */ /** A for statement, such as `for x in y: print(x)` */
class For extends For_ { class For extends For_ {
/* syntax: for varname in Expr: */
override Stmt getASubStatement() { override Stmt getASubStatement() {
result = this.getAStmt() or result = this.getAStmt() or
result = this.getAnOrelse() result = this.getAnOrelse()
@ -279,6 +302,8 @@ class For extends For_ {
/** A global statement, such as `global var` */ /** A global statement, such as `global var` */
class Global extends Global_ { class Global extends Global_ {
/* syntax: global varname */
override Expr getASubExpression() { override Expr getASubExpression() {
none() none()
} }
@ -291,6 +316,8 @@ class Global extends Global_ {
/** An if statement, such as `if eggs: print("spam")` */ /** An if statement, such as `if eggs: print("spam")` */
class If extends If_ { class If extends If_ {
/* syntax: if Expr: ... */
override Stmt getASubStatement() { override Stmt getASubStatement() {
result = this.getAStmt() or result = this.getAStmt() or
result = this.getAnOrelse() result = this.getAnOrelse()
@ -345,6 +372,8 @@ class If extends If_ {
/** A nonlocal statement, such as `nonlocal var` */ /** A nonlocal statement, such as `nonlocal var` */
class Nonlocal extends Nonlocal_ { class Nonlocal extends Nonlocal_ {
/* syntax: nonlocal varname */
override Stmt getASubStatement() { override Stmt getASubStatement() {
none() none()
} }
@ -363,6 +392,8 @@ class Nonlocal extends Nonlocal_ {
/** A pass statement */ /** A pass statement */
class Pass extends Pass_ { class Pass extends Pass_ {
/* syntax: pass */
override Stmt getASubStatement() { override Stmt getASubStatement() {
none() none()
} }
@ -376,6 +407,8 @@ class Pass extends Pass_ {
/** A print statement (Python 2 only), such as `print 0` */ /** A print statement (Python 2 only), such as `print 0` */
class Print extends Print_ { class Print extends Print_ {
/* syntax: print Expr, ... */
override Stmt getASubStatement() { override Stmt getASubStatement() {
none() none()
} }
@ -390,6 +423,8 @@ class Print extends Print_ {
/** A raise statement, such as `raise CompletelyDifferentException()` */ /** A raise statement, such as `raise CompletelyDifferentException()` */
class Raise extends Raise_ { class Raise extends Raise_ {
/* syntax: raise Expr */
override Stmt getASubStatement() { override Stmt getASubStatement() {
none() none()
} }
@ -424,6 +459,8 @@ class Raise extends Raise_ {
/** A return statement, such as return None */ /** A return statement, such as return None */
class Return extends Return_ { class Return extends Return_ {
/* syntax: return Expr */
override Stmt getASubStatement() { override Stmt getASubStatement() {
none() none()
} }
@ -437,6 +474,8 @@ class Return extends Return_ {
/** A try statement */ /** A try statement */
class Try extends Try_ { class Try extends Try_ {
/* syntax: try: ... */
override Expr getASubExpression() { override Expr getASubExpression() {
none() none()
} }
@ -475,6 +514,8 @@ class Try extends Try_ {
/** A while statement, such as `while parrot_resting():` */ /** A while statement, such as `while parrot_resting():` */
class While extends While_ { class While extends While_ {
/* syntax: while Expr: ... */
override Expr getASubExpression() { override Expr getASubExpression() {
result = this.getTest() result = this.getTest()
} }
@ -496,6 +537,8 @@ class While extends While_ {
/** A with statement such as `with f as open("file"): text = f.read()` */ /** A with statement such as `with f as open("file"): text = f.read()` */
class With extends With_ { class With extends With_ {
/* syntax: with Expr as varname: ... */
override Expr getASubExpression() { override Expr getASubExpression() {
result = this.getContextExpr() or result = this.getContextExpr() or
result = this.getOptionalVars() result = this.getOptionalVars()
@ -526,6 +569,8 @@ class TemplateWrite extends TemplateWrite_ {
class AsyncFor extends For { class AsyncFor extends For {
/* syntax: async for varname in Expr: */
AsyncFor() { AsyncFor() {
this.isAsync() this.isAsync()
} }
@ -534,6 +579,8 @@ class AsyncFor extends For {
class AsyncWith extends With { class AsyncWith extends With {
/* syntax: async with Expr as varname: ... */
AsyncWith() { AsyncWith() {
this.isAsync() this.isAsync()
} }