Skip to content

Commit a256945

Browse files
committed
Python: Add syntax example comments for document generation.
1 parent 4f26b58 commit a256945

File tree

7 files changed

+125
-0
lines changed

7 files changed

+125
-0
lines changed

python/ql/src/semmle/python/AstExtended.qll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import python
33
/** Syntactic node (Class, Function, Module, Expr, Stmt or Comprehension) corresponding to a flow node */
44
abstract class AstNode extends AstNode_ {
55

6+
/* Special comment for documentation generation */
7+
/* syntax: */
8+
69
/** Gets the scope that this node occurs in */
710
abstract Scope getScope();
811

@@ -206,6 +209,8 @@ class ComprehensionList extends ComprehensionList_ {
206209
/** A list of expressions */
207210
class ExprList extends ExprList_ {
208211

212+
/* syntax: Expr, ... */
213+
209214
}
210215

211216

python/ql/src/semmle/python/Class.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ class ClassExpr extends ClassExpr_ {
6363
/** A class statement. Note that ClassDef extends Assign as a class definition binds the newly created class */
6464
class ClassDef extends Assign {
6565

66+
/* syntax: class name(...): ... */
67+
6668
ClassDef() {
6769
/* This is an artificial assignment the rhs of which is a (possibly decorated) ClassExpr */
6870
exists(ClassExpr c | this.getValue() = c or this.getValue() = c.getADecoratorCall())

python/ql/src/semmle/python/Exprs.qll

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ class Expr extends Expr_, AstNode {
131131
/** An attribute expression, such as `value.attr` */
132132
class Attribute extends Attribute_ {
133133

134+
/* Special comment for documentation generation */
135+
/* syntax: Expr.name */
136+
134137
override Expr getASubExpression() {
135138
result = this.getObject()
136139
}
@@ -160,6 +163,8 @@ class Attribute extends Attribute_ {
160163
/** A subscript expression, such as `value[slice]` */
161164
class Subscript extends Subscript_ {
162165

166+
/* syntax: Expr[Expr] */
167+
163168
override Expr getASubExpression() {
164169
result = this.getIndex()
165170
or
@@ -176,6 +181,8 @@ class Subscript extends Subscript_ {
176181
/** A call expression, such as `func(...)` */
177182
class Call extends Call_ {
178183

184+
/* syntax: Expr(...) */
185+
179186
override Expr getASubExpression() {
180187
result = this.getAPositionalArg() or
181188
result = this.getAKeyword().getValue() or
@@ -268,6 +275,8 @@ class Call extends Call_ {
268275
/** A conditional expression such as, `body if test else orelse` */
269276
class IfExp extends IfExp_ {
270277

278+
/* syntax: Expr if Expr else Expr */
279+
271280
override Expr getASubExpression() {
272281
result = this.getTest() or result = this.getBody() or result = this.getOrelse()
273282
}
@@ -278,6 +287,8 @@ class IfExp extends IfExp_ {
278287
/** A starred expression, such as the `*rest` in the assignment `first, *rest = seq` */
279288
class Starred extends Starred_ {
280289

290+
/* syntax: *Expr */
291+
281292
override Expr getASubExpression() {
282293
result = this.getValue()
283294
}
@@ -288,6 +299,8 @@ class Starred extends Starred_ {
288299
/** A yield expression, such as `yield value` */
289300
class Yield extends Yield_ {
290301

302+
/* syntax: yield Expr */
303+
291304
override Expr getASubExpression() {
292305
result = this.getValue()
293306
}
@@ -301,6 +314,8 @@ class Yield extends Yield_ {
301314
/** A yield expression, such as `yield from value` */
302315
class YieldFrom extends YieldFrom_ {
303316

317+
/* syntax: yield from Expr */
318+
304319
override Expr getASubExpression() {
305320
result = this.getValue()
306321
}
@@ -314,6 +329,8 @@ class YieldFrom extends YieldFrom_ {
314329
/** A repr (backticks) expression, such as `` `value` `` */
315330
class Repr extends Repr_ {
316331

332+
/* syntax: `Expr` */
333+
317334
override Expr getASubExpression() {
318335
result = this.getValue()
319336
}
@@ -330,6 +347,8 @@ class Repr extends Repr_ {
330347
`"hello"` are treated as Bytes for Python2, but Unicode for Python3. */
331348
class Bytes extends StrConst {
332349

350+
/* syntax: b"hello" */
351+
333352
Bytes() {
334353
not this.isUnicode()
335354
}
@@ -355,6 +374,8 @@ class Bytes extends StrConst {
355374
/** An ellipsis expression, such as `...` */
356375
class Ellipsis extends Ellipsis_ {
357376

377+
/* syntax: ... */
378+
358379
override Expr getASubExpression() {
359380
none()
360381
}
@@ -394,6 +415,8 @@ abstract class Num extends Num_, ImmutableLiteral {
394415
/** An integer numeric constant, such as `7` or `0x9` */
395416
class IntegerLiteral extends Num {
396417

418+
/* syntax: 4 */
419+
397420
IntegerLiteral() {
398421
not this instanceof FloatLiteral and not this instanceof ImaginaryLiteral
399422
}
@@ -425,6 +448,8 @@ class IntegerLiteral extends Num {
425448
/** A floating point numeric constant, such as `0.4` or `4e3` */
426449
class FloatLiteral extends Num {
427450

451+
/* syntax: 4.2 */
452+
428453
FloatLiteral() {
429454
not this instanceof ImaginaryLiteral and
430455
this.getN().regexpMatch(".*[.eE].*")
@@ -456,6 +481,9 @@ class FloatLiteral extends Num {
456481
class ImaginaryLiteral extends Num {
457482
private float value;
458483

484+
/* Special comment for documentation generation */
485+
/* syntax: 1.0j */
486+
459487
ImaginaryLiteral() {
460488
value = this.getN().regexpCapture("(.+)j.*", 1).toFloat()
461489
}
@@ -513,6 +541,9 @@ class NegativeIntegerLiteral extends ImmutableLiteral, UnaryExpr {
513541
"hello" are treated as Bytes for Python2, but Unicode for Python3. */
514542
class Unicode extends StrConst {
515543

544+
/* Special comment for documentation generation */
545+
/* syntax: "hello" */
546+
516547
Unicode() {
517548
this.isUnicode()
518549
}
@@ -541,6 +572,9 @@ class Unicode extends StrConst {
541572
/** A dictionary expression, such as `{'key':'value'}` */
542573
class Dict extends Dict_ {
543574

575+
/* Special comment for documentation generation */
576+
/* syntax: {Expr: Expr, ...} */
577+
544578
/** Gets the value of an item of this dict display */
545579
Expr getAValue() {
546580
result = this.getAnItem().(DictDisplayItem).getValue()
@@ -566,6 +600,9 @@ class Dict extends Dict_ {
566600
/** A list expression, such as `[ 1, 3, 5, 7, 9 ]` */
567601
class List extends List_ {
568602

603+
/* Special comment for documentation generation */
604+
/* syntax: [Expr, ...] */
605+
569606
override Expr getASubExpression() {
570607
result = this.getAnElt()
571608
}
@@ -575,6 +612,9 @@ class List extends List_ {
575612
/** A set expression such as `{ 1, 3, 5, 7, 9 }` */
576613
class Set extends Set_ {
577614

615+
/* Special comment for documentation generation */
616+
/* syntax: {Expr, ...} */
617+
578618
override Expr getASubExpression() {
579619
result = this.getAnElt()
580620
}
@@ -601,6 +641,8 @@ class PlaceHolder extends PlaceHolder_ {
601641
/** A tuple expression such as `( 1, 3, 5, 7, 9 )` */
602642
class Tuple extends Tuple_ {
603643

644+
/* syntax: (Expr, ...) */
645+
604646
override Expr getASubExpression() {
605647
result = this.getAnElt()
606648
}
@@ -612,6 +654,8 @@ class Tuple extends Tuple_ {
612654
*/
613655
class Name extends Name_ {
614656

657+
/* syntax: name */
658+
615659
string getId() {
616660
result = this.getVariable().getId()
617661
}
@@ -705,6 +749,9 @@ class Slice extends Slice_ {
705749
/** A string constant. */
706750
class StrConst extends Str_, ImmutableLiteral {
707751

752+
/* Special comment for documentation generation */
753+
/* syntax: "hello" */
754+
708755
predicate isUnicode() {
709756
this.getPrefix().charAt(_) = "u"
710757
or
@@ -790,6 +837,9 @@ abstract class BooleanLiteral extends NameConstant {
790837
/** The boolean named constant `True` */
791838
class True extends BooleanLiteral {
792839

840+
/* Special comment for documentation generation */
841+
/* syntax: True */
842+
793843
True() {
794844
name_consts(this, "True")
795845
}
@@ -807,6 +857,9 @@ class True extends BooleanLiteral {
807857
/** The boolean named constant `False` */
808858
class False extends BooleanLiteral {
809859

860+
/* Special comment for documentation generation */
861+
/* syntax: False */
862+
810863
False() {
811864
name_consts(this, "False")
812865
}
@@ -824,6 +877,9 @@ class False extends BooleanLiteral {
824877
/** `None` */
825878
class None extends NameConstant {
826879

880+
/* Special comment for documentation generation */
881+
/* syntax: None */
882+
827883
None() {
828884
name_consts(this, "None")
829885
}
@@ -840,6 +896,9 @@ class None extends NameConstant {
840896
/** An await expression such as `await coro`. */
841897
class Await extends Await_ {
842898

899+
/* Special comment for documentation generation */
900+
/* syntax: await Expr */
901+
843902
override Expr getASubExpression() {
844903
result = this.getValue()
845904
}
@@ -849,6 +908,8 @@ class Await extends Await_ {
849908
/** A formatted string literal expression, such as `f'hello {world!s}'` */
850909
class Fstring extends Fstring_ {
851910

911+
/* syntax: f"Yes!" */
912+
852913
override Expr getASubExpression() {
853914
result = this.getAValue()
854915
}

python/ql/src/semmle/python/Function.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ class Function extends Function_, Scope, AstNode {
187187
/** A def statement. Note that FunctionDef extends Assign as a function definition binds the newly created function */
188188
class FunctionDef extends Assign {
189189

190+
/* syntax: def name(...): ... */
191+
190192
FunctionDef() {
191193
/* This is an artificial assignment the rhs of which is a (possibly decorated) FunctionExpr */
192194
exists(FunctionExpr f | this.getValue() = f or this.getValue() = f.getADecoratorCall())

python/ql/src/semmle/python/Import.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ class ImportMember extends ImportMember_ {
170170
/** An import statement */
171171
class Import extends Import_ {
172172

173+
/* syntax: import modname */
174+
173175
private ImportExpr getAModuleExpr() {
174176
result = this.getAName().getValue()
175177
or
@@ -222,6 +224,8 @@ class Import extends Import_ {
222224
/** An import * statement */
223225
class ImportStar extends ImportStar_ {
224226

227+
/* syntax: from modname import * */
228+
225229
ImportExpr getModuleExpr() {
226230
result = this.getModule()
227231
or

python/ql/src/semmle/python/Keywords.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import python
22

33
class KeyValuePair extends KeyValuePair_, DictDisplayItem {
44

5+
/* syntax: Expr : Expr */
6+
57
override Location getLocation() {
68
result = KeyValuePair_.super.getLocation()
79
}
@@ -76,6 +78,8 @@ abstract class DictDisplayItem extends DictItem {
7678
/** A keyword argument in a call. For example `arg=expr` in `foo(0, arg=expr)` */
7779
class Keyword extends Keyword_, DictUnpackingOrKeyword {
7880

81+
/* syntax: name = Expr */
82+
7983
override Location getLocation() {
8084
result = Keyword_.super.getLocation()
8185
}

0 commit comments

Comments
 (0)